import React, { Component } from 'react';
import { Input, Button, Row, Col, Modal, Form, Upload, Checkbox, InputNumber, Spin, Select, message } from 'antd';
// import message from '@wanhu/antd-legacy/lib/message';
import AsyncEvent from '../../common/AsyncEvent';
import { connect } from '../../../states/insurance/createPatientInsurance';
import Title from '../../common/Title';
import { SmartSelectSingleAsync } from '../../common/SmartSelectSingle';
import { connectModalHelper } from '../../../mixins/modal';
import SmartSelectBox from '../../common/SmartSelectBox';
import api from '../../../api/api';
import IDValidator from '../../../helpers/checkIdCard';
import setTimeout from '../../../helpers/timeout';
import PatientcertIficationImg from '../../../images/patientcertIfication.png';
import Viewer from '../../toolcase/Viewer';
import { debounce, cloneDeep } from 'lodash';
import HasPermission, { testPermission } from '../../common/HasPermission';
import { markIdCard, markPhoneNumber } from '../../../utils';
// import jsInquiryData from '../../inquiry'
import './index.less';
const { TextArea } = Input;

const { log } = console;
const { Option } = Select;
const FormItem = Form.Item;
const formItemLayout = {
  labelCol: { span: 8 },
  wrapperCol: { span: 16 },
};
const formItemStyle = {
  labelCol: { span: 4 },
  wrapperCol: { span: 20 },
};
const mapPropsToFormItems = {
  editStatus: true,
  notEditableOnly: false,
};
const colStyle = {
  textOverflow: 'ellipsis',
  overflow: 'hidden',
  whiteSpace: 'nowrap',
};

let win; // window.open 打开的窗口对象

// 环境区分
function envType() {
  const host = location.host;
  let inquiryLink;
  let e;

  if (host === 't.crm.wanhuhealth.com' || host === 't-crm.wanhuhealth.com') {
    inquiryLink = 'https://d-bx.wanhuhealth.com/common/index.html';
    e = 't';
  } else if (host === 'demo-crm.wanhuhealth.com') {
    inquiryLink = 'https://d-bx.wanhuhealth.com/common/index.html';
    e = 'd';
  } else if (host === 'localhost:7003') {
    inquiryLink = 'https://d-bx.wanhuhealth.com/common/index.html';
    // inquiryLink = 'http://192.168.1.246:8080/common/index.html';
    e = 't';
  } else {
    inquiryLink = 'https://bx.wanhuhealth.com/common/index.html';
    e = 'p';
  }

  return {
    inquiryLink,
    e,
  };
}

function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
}

function getAgeByBirthday(birthday) {
  var nowDate = new Date();
  var birDate = new Date(birthday.replace(/\-/g, '/'));
  var month = nowDate.getMonth() + 1;
  var day = nowDate.getDate();
  var age = nowDate.getFullYear() - birDate.getFullYear();
  var birMonth = birDate.getMonth() + 1;
  var birDay = birDate.getDate();
  if (birMonth < month || (birMonth == month && birDay <= day)) {
    age;
  } else if (birMonth == month && birDay == day + 1) {
    age--;
  }
  return age;
}

function IdCardCheck(UUserCard) {
  //获取年龄
  var myDate = new Date();
  var month = myDate.getMonth() + 1;
  var day = myDate.getDate();
  var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
  if (
    UUserCard.substring(10, 12) < month ||
    (UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day)
  ) {
    age++;
  } else if (UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) == day + 1) {
    age++;
  }
  return age;
}
// 获取购买人历史记录的接口换成了购买人家庭成员接口，因此要对数据进行转换
/**
 *
"insuredName":"",//被保人姓名
"insuredId":"",//被保人ID
"relationShip":1,//关系 1本人 2配偶 3 父母 4 子女
"insuredIdCard":"",//被保人身份证号
"insuredPhone":""//被保人手机号
 */

/**
 *
"patientName": "安新", //家庭成员姓名
"memberId": "b11c48dbf52f40ef9673bbd085b8d580", //家庭成员id
"relation": 1, //会员关系 1=本人 2=配偶 3=父母 4=子女
 */
function covertFamilyMember2HisMember(familyMemberList) {
  if (Array.isArray(familyMemberList)) {
    return familyMemberList.map((item) => {
      return {
        insuredName: item.patientName,
        insuredId: item.memberId,
        relationShip: item.relation,
        insuredIdCard: item.idCard,
        insuredPhone: item.phone,
        birthday: item.birthday,
      };
    });
  } else {
    return [];
  }
}

function getCookie(key) {
  //以分号分割cookie并返回一个数组
  var res = document.cookie.split(';');
  for (let i = 0; i < res.length; i++) {
    var temp = res[i].split('=');
    if (temp[0].trim() === key) {
      return temp[1];
    }
  }
}

class CreatPatientInsuranceOrder extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isSubmiting: false,
      diseaseList: [],
      loading: true,
      isEdit: false,
      isChecked: false,
      povertyPicList: [],
      authorizationPicList: [],
      insurancePackageProList: [],
      insurancePackagePro: null,
      insuranceAccountInfo: null,
      isProhibit: false,
      povertyValidate: null,
      payPrice: null,
      isgeting: false,
      payWay: ['1'],
      location_href: 0,
      q_list: {},
      ischange: 1,
      iscomplete: false,
      releation_ship_list: [],
      channelCode: '', //渠道码
      loading: false,
      channelCodeData: [],
      channelisCodeDis: true,
      dlgTitle: '登记会员服务', // 弹窗标题
      inquiryData: null, // 是否需要第三方问询
      ssOwners: [], // 服务专员列表
      userInfo: {}, // 当前登录人信息
      curSsOwnerId: '', // 当前服务专员id
      curSsOwner: null,
      isInsurance: false, // 服务包是否含保险
      remarks: '', // 备注,
      orderInfo: null, // 创建的服务单信息
    };
  }

  async componentDidMount() {
    this.initList();
    //检查渠道修改权限是否允许
    if (testPermission('insurance.edit.channelcode')) {
      this.setState({
        channelisCodeDis: false,
      });
    }
    // 获取服务专员列表
    let ssOwners = await api.getChannelCode();
    // 获取当前登录人信息
    let userInfo = await api.getUserInfo();
    this.setState({
      ssOwners,
      userInfo,
      curSsOwnerId: userInfo.user,
    });

    window.addEventListener('message', this.postMsg, false);
  }

  componentWillUnmount() {
    window.removeEventListener('message', this.postMsg, false);
  }

  async initList() {
    const selectedInsurance =
      this.props.getSelectedInsurance &&
      this.props.getSelectedInsurance.status &&
      this.props.getSelectedInsurance.status.selectedInsurance;
    const selectedInsuranceId =
      this.props.getSelectedInsuranceId &&
      this.props.getSelectedInsuranceId.status &&
      this.props.getSelectedInsuranceId.status.insuranceSelectedId;
    const { form, getInsuracePackageAction, getInsuracePackageResult } = this.props;
    getInsuracePackageAction();
    if (selectedInsuranceId) {
      this.props.form.setFieldsValue({ insuranceProductId: selectedInsuranceId });
      this.setState({
        isSubmiting: false,
        selectedInsuranceId,
      });
    }
    //获取当前用户的渠道码
    let user = JSON.parse(window.localStorage.getItem('userInfo'));
    let data = await api.getChannelCode({});

    const data2 = data.map((el) => ({
      text: `${el.name}(${el.channelCode})-${el.companyName}`,
      value: `${el.channelCode}`,
    }));
    //设置默认
    const moren = data.filter((item) => item.userId == user.user);
    this.setState({
      channelCode: moren[0].channelCode,
      channelCodeData: [...data2],
    });
  }

  async init(patientId) {
    const { getPatientAction, getInsuranceSubmitData } = this.props;
    try {
      getPatientAction(patientId);
    } catch (e) {
      console.error(e);
      message.error(e.message);
    }
    try {
      //查询会员账户
      const insuranceAccountInfo = await api.insuranceAccountInfo(patientId);
      this.setState({ insuranceAccountInfo });
    } catch (e) {
      console.error('catched error--查询会员账户:', e);
    }
    this.setState({ healthInquiryResults: null, healthInquiryValidate: null, diseaseList: [] });
  }

  async componentWillReceiveProps(nextProps) {
    const thisPatientResultStatus = this.props.getPatientResult.status;
    const nextPatientResultStatus = nextProps.getPatientResult.status;
    const { selectedInfo } = this.state;
    if (
      nextPatientResultStatus &&
      thisPatientResultStatus &&
      thisPatientResultStatus.status !== nextPatientResultStatus.status &&
      nextPatientResultStatus.status === 'fulfilled'
    ) {
      const { setFieldsValue, validateFields } = nextProps.form;
      const patientDetails = nextPatientResultStatus.payload;
      let isSuc = true;
      if (!patientDetails.certification) {
        isSuc = await this.checkId(patientDetails, this.patientId);
      }
      // 判断服务包是否含有保险
      const isInsurance = selectedInfo
        ? selectedInfo.insurancePackagePros &&
          selectedInfo.insurancePackagePros[0].products &&
          selectedInfo.insurancePackagePros[0].products.some((item) => item.type === 1)
        : false;
      this.setState({
        isInsurance: isInsurance,
      });

      if (!patientDetails.phone) {
        message.error('手机号不存在，请重新维护该会员信息');
        this.setState({
          patientFieldValue: undefined,
          patientDetails: undefined,
          selPatientRelValue: undefined,
          selPatientRelDetails: undefined,
          isgeting: false,
        });
        return;
      }
      // const { selectedInfo } = this.state;
      let new_cityId = null;
      let new_provinceId = null;
      // if(patientDetails && patientDetails.memberType == 1){ // 保障
      new_cityId = (patientDetails.hospital && patientDetails.hospital.cityId) || null;
      new_provinceId = (patientDetails.hospital && patientDetails.hospital.provinceId) || null;
      /* }else{ // 绿a
                new_cityId = patientDetails.address.liveCity;
                new_provinceId = patientDetails.address.liveProvinces
            } */
      if (
        selectedInfo &&
        selectedInfo.areas &&
        selectedInfo.areas.length &&
        !selectedInfo.areas.find((item) =>
          item.cityId ? item.cityId == new_cityId : item.provincesId ? item.provincesId == new_provinceId : null,
        )
      ) {
        message.error('购买人所在城市与服务包服务区域不符');
        this.setState({
          patientFieldValue: undefined,
          patientDetails: undefined,
          selPatientRelValue: undefined,
          selPatientRelDetails: undefined,
          isgeting: false,
        });
        return;
      }
      const age = patientDetails.birthday ? `/${getAgeByBirthday(patientDetails.birthday)}岁` : '';
      const label = `${patientDetails.name} (${patientDetails.sex ? '男' : '女'}${age}) ${
        markPhoneNumber(patientDetails.phone) || ''
      } ${patientDetails.machineNumber || ''} ${patientDetails.address.liveStreet || ''} ${
        patientDetails.hospital.name || ''
      }`;
      const patientFieldValue = {
        key: this.patientId,
        label,
      };
      this.setState({ patientFieldValue, patientDetails, isgeting: false });
      /* setFieldsValue({
                patientName: patientDetails.name,
                idCard: patientDetails.idCard,
            }); */
      if (patientDetails.certification) {
        this.getInsuracePackage();
      } else {
        validateFields(['patientName', 'idCard']);
      }
    }

    // const nextInsurancePackageResultStatus = nextProps.getInsuracePackageResult.status;
    // if (nextInsurancePackageResultStatus && nextInsurancePackageResultStatus.status === 'fulfilled') {
    //     const insuranceIndexPackageList = nextInsurancePackageResultStatus.payload;
    //     this.setState({
    //         insuranceIndexPackageList,
    //         insuranceIndexPackageList2: cloneDeep(insuranceIndexPackageList),
    //         loading: false
    //     });
    // }

    const thisSelPatientResultStatus = this.props.getRelPatient.status;
    const nextSelPatientResultStatus = nextProps.getRelPatient.status;
    if (
      nextSelPatientResultStatus &&
      thisSelPatientResultStatus &&
      thisSelPatientResultStatus.status !== nextSelPatientResultStatus.status &&
      nextSelPatientResultStatus.status === 'fulfilled'
    ) {
      const patientDetails = nextSelPatientResultStatus.payload;

      let isSuc = true;

      // 如果服务包有保险
      if (!patientDetails.certification && this.state.isInsurance) {
        isSuc = await this.checkId(patientDetails, this.relPatientId);

        if (!isSuc) {
          message.error('服务对象实名认证失败，请确认信息后重新维护该会员信息');
          this.setState({
            selPatientRelValue: undefined,
            selPatientRelDetails: undefined,
            isgeting: false,
          });
          return;
        }
      }

      if (!patientDetails.phone) {
        message.error('手机号不存在，请重新维护该会员信息');
        this.setState({
          selPatientRelValue: undefined,
          selPatientRelDetails: undefined,
          isgeting: false,
        });
        return;
      }
      const age = patientDetails.birthday ? `/${getAgeByBirthday(patientDetails.birthday)}岁` : '';
      if (
        age - this.state.insuranceSelectedOrder.ageRange[0] < 0 ||
        age - this.state.insuranceSelectedOrder.ageRange[1] > 0
      ) {
        message.error('服务对象年龄与服务包限定范围不符，不可购买');
        this.setState({
          selPatientRelValue: undefined,
          selPatientRelDetails: undefined,
          isgeting: false,
        });
        return;
      }
      const label = `${patientDetails.name} (${patientDetails.sex ? '男' : '女'}${age}) ${patientDetails.phone || ''} ${
        patientDetails.machineNumber || ''
      } ${patientDetails.address.liveStreet || ''} ${patientDetails.hospital.name || ''}`;
      const selPatientRelValue = {
        key: nextSelPatientResultStatus.params.patientId,
        label,
      };
      this.setState({ selPatientRelValue, selPatientRelDetails: patientDetails, isgeting: false });
    }
  }

  async checkId(data, id) {
    await this.setPromise({
      isgeting: true,
    });
    try {
      await api.checkPatientIdCard(id, data.name, data.idCard);
      this.setState({
        isgeting: false,
      });
      return true;
    } catch (e) {
      return false;
    }
  }

  setPromise(data) {
    return new Promise((resolve, reject) => {
      this.setState(data, resolve);
    });
  }

  // async initData(data) {
  //     console.log('initData data:',data)
  //     const { form, getPatientAction } = this.props;
  //     const { setFieldsValue } = form;
  //     const patientList_1 = data.f_patient ? getPatientAction(data.f_patient) : null;
  //     this.patientId = data.f_patient ? data.f_patient : ''
  //     const patientList_2 = data.s_patient ? getPatientAction(data.s_patient) : null;
  //     let hisCusterList = [];
  //     if (data.f_patient) {
  //         // hisCusterList = await api.getHistoryPatientInfo(data.f_patient);
  //         // 改成获取家庭成员
  //         hisCusterList = await api.getPatientFamilyMember(data.f_patient);
  //         hisCusterList = covertFamilyMember2HisMember(hisCusterList)
  //     }
  //     const selectedInsurance = this.props.getSelectedInsurance && this.props.getSelectedInsurance.status && this.props.getSelectedInsurance.status.selectedInsurance;
  //     const insurancePackagePro = selectedInsurance.insurancePackagePros.find(item => item.id == data.insurancePackageProsId)
  //     this.payWayOnChange(null, insurancePackagePro)
  //     this.setState({
  //         patientFieldValue: data.patientFieldValue,
  //         selPatientReleation: data.selPatientReleation,
  //         selPatientRelValue: data.selPatientRelValue,
  //         authorizationPicList: data.authorizationPicList,
  //         choseInsurancePatient: data.choseInsurancePatient,
  //         guarantee: data.guarantee,
  //         poverty: data.poverty,
  //         insurancePackageProsId: data.insurancePackageProsId || '',
  //         payWay: data.payWay,
  //         hisCusterList,
  //         insurancePackageProList: selectedInsurance.insurancePackagePros,
  //         insurancePackagePro,
  //         isCash: data.isCash,
  //         is3dCash: data.is3dCash,
  //         isPoint: data.isPoint,
  //         isInsuranceAccount: data.isInsuranceAccount,
  //         patientDetails: data.f_patient ? patientList_1 : '',
  //         selPatientRelDetails: data.s_patient ? patientList_2 : '',
  //         payPoint: data.payPoint,
  //         payInsuranceAccount: data.payInsuranceAccount,
  //         payCash: data.payCash,
  //         pay3dCash: data.pay3dCash,
  //         payPrice: data.payPrice,
  //         insuranceSelectedOrder: selectedInsurance,
  //         buyCode: data.buyCode,
  //         subCode: data.subCode,
  //     })
  // }

  checkName = async (rule, value, callback) => {
    if (!value) {
      callback();
      return;
    }
    //没有选择患者
    if (!this.patientId) {
      callback();
      return;
    }
    const data = this.props.getPatientResult.status.payload;
    if (data && data.certification) {
      callback();
      return;
    }
    callback();
  };

  checkIdCard = async (rule, value, callback) => {
    if (!value) {
      callback();
      return;
    }
    //没有选择患者
    if (!this.patientId) {
      callback();
      return;
    }
    const data = this.props.getPatientResult.status.payload;
    if (data && data.certification) {
      callback();
      return;
    }
    const { getFieldValue } = this.props.form;
    const validator = IDValidator;
    const valueStr = String(value);
    if (!data) {
      if (validator.isValid(valueStr)) {
      } else {
        callback('请输入正确的身份证号');
      }
      callback();
    } else {
      if (validator.isValid(valueStr)) {
        callback();
      } else if (data.signStatus !== 0) {
        callback('请输入正确的身份证号');
      }
    }
    this.getInsuracePackage();
    callback();
  };

  checkPovertyPicList = async (rule, value, callback) => {
    const { povertyPicList } = this.state;
    if (!povertyPicList || povertyPicList.length <= 0) {
      callback('不能为空');
      return;
    }
    callback();
  };

  checkAuthorizationPicList = async (rule, value, callback) => {
    const { authorizationPicList } = this.state;
    if (!authorizationPicList || authorizationPicList.length <= 0) {
      callback('不能为空');
      return;
    }
    callback();
  };

  payWayOnChange = (v, pro) => {
    //计算会员服务费
    const { patientDetails } = this.state;
    const insurancePackagePro = pro || this.state.insurancePackagePro;
    const { form } = this.props;
    const { getFieldValue } = form;
    const payWay = v || this.state.payWay;
    let totalPrice = insurancePackagePro ? insurancePackagePro.salesPrice : 0;
    if (
      patientDetails &&
      patientDetails.gradeId &&
      insurancePackagePro &&
      insurancePackagePro.memeberGradePrices &&
      insurancePackagePro.memeberGradePrices[patientDetails.gradeId] !== undefined
    ) {
      const { memeberGradePrices } = insurancePackagePro;
      totalPrice =
        memeberGradePrices[patientDetails.gradeId] >= 0 ? memeberGradePrices[patientDetails.gradeId] : payPrice;
    }
    this.setState({ totalPrice });
    let payPrice = totalPrice;
    if (payWay && payWay.length > 0 && payWay[0] === '2') {
      payPrice = (Math.ceil(totalPrice / 100 / 12) * 100).toFixed(0);
      this.setState({ payPrice, isPoint: [1], isInsuranceAccount: [1] }, () => {
        this.countOnchange([1], 'isPoint');
        this.countOnchange([1], 'isInsuranceAccount');
      });
    } else {
      this.setState({ payPrice: totalPrice }, () => {
        this.countOnchange();
      });
    }
  };

  countOnchange = (v, key) => {
    console.log('countOnchange v:', v);
    console.log('countOnchange key:', key);
    const { patientDetails } = this.state;
    if (!patientDetails) {
      return;
    }
    if (key === 'isPoint') {
      this.setState({
        isPoint: v,
      });
    } else if (key === 'isInsuranceAccount') {
      this.setState({
        isInsuranceAccount: v,
      });
    } else if (key === 'isCash') {
      this.setState({
        isCash: v,
      });
    } else if (key === 'is3dCash') {
      this.setState({
        is3dCash: v,
      });
    }
    const { form } = this.props;
    const { getFieldValue } = form;
    const { insuranceAccountInfo, payPrice } = this.state;
    let needPayPrice = payPrice;
    let payPoint = 0;
    let payInsuranceAccount = 0;
    let payCash = 0;
    let pay3dCash = 0;
    const isPoint = key === 'isPoint' ? v : this.state.isPoint;
    if (isPoint && isPoint.length > 0 && isPoint[0] === 1) {
      payPoint = (patientDetails.points || 0) >= needPayPrice ? needPayPrice : patientDetails.points || 0;
      needPayPrice -= payPoint;
    }
    const isInsuranceAccount = key === 'isInsuranceAccount' ? v : this.state.isInsuranceAccount;
    if (isInsuranceAccount && isInsuranceAccount.length > 0 && isInsuranceAccount[0] === 1) {
      const balance = insuranceAccountInfo ? insuranceAccountInfo.balance || 0 : 0;
      payInsuranceAccount = balance >= needPayPrice ? needPayPrice : balance;
      needPayPrice -= payInsuranceAccount;
    }
    const isCash = key === 'isCash' ? v : this.state.isCash;
    // 第三方支付
    const is3dCash = key === 'is3dCash' ? v : this.state.is3dCash;
    if (isCash && isCash.length > 0 && isCash[0] === 1) {
      payCash = needPayPrice;
    } else if (is3dCash && is3dCash.length > 0 && is3dCash[0] === 1) {
      pay3dCash = needPayPrice;
    }

    this.setState({ payPoint, payInsuranceAccount, payCash, pay3dCash });
  };

  hideGroupModal = () => {
    this.reset();
    this.restForm();
    this.props.closeModal();
  };

  onCancel = () => {
    this.hideGroupModal();
  };

  finishCreatePatientInsuranceOrder = () => {
    const { patientDetails } = this.state;
    const { setPatientInsuranceResult } = this.props;
    setPatientInsuranceResult(patientDetails);
    this.hideGroupModal();
    this.props.openModal('patientInsuranceResult');
  };

  restForm() {
    const {
      setInsuraceSelected,
      setSelectedInsuranceId,
      setInsuranceSubmitData,
      setToNextData,
      saveDiseaseInfo,
      savePutData,
    } = this.props;
    setInsuraceSelected(null);
    setSelectedInsuranceId(null);
    setInsuranceSubmitData(null);
    setToNextData(null);
    saveDiseaseInfo(null);
    savePutData(null);
  }

  mapDataToOption = (data) => {
    this.patientOptionData = data;
    if (!data.list) return null;
    const options = data.list.map((row) => {
      const age = row.birthday ? '/' + getAgeByBirthday(row.birthday) + '岁' : '';
      let sexOption = { 1: '男', 0: '女' };
      let sexStr = sexOption[row.sex] ? sexOption[row.sex] : '';
      const name = `${row.name} ${sexStr || age ? '(' + sexStr + age + ')' : ''}`;
      return (
        <Option key={row.id}>
          <Row key={row.id} gutter={10}>
            <Col title={name} style={colStyle} span={3}>
              {name}
            </Col>
            <Col title={name} style={colStyle} span={3}>
              <img style={{ maxHeight: 30 }} src={row.levelIcon} />
            </Col>
            <Col title={name} style={colStyle} span={2}>
              <img style={{ width: 32 }} src={row.isCertification ? PatientcertIficationImg : ''} />
            </Col>
            <Col title={row.phone || ''} style={colStyle} span={4}>
              {row.phone || ''}
            </Col>
            <Col title={row.machineNumber || ''} style={colStyle} span={3}>
              {row.machineNumber || ''}
            </Col>
            <Col title={row.address.liveStreet} style={colStyle} span={4}>
              {row.address.liveStreet}
            </Col>
            <Col title={row.hospitalName} style={colStyle} span={5}>
              {row.hospitalName}
            </Col>
          </Row>
        </Option>
      );
    });
    if (data.list.length === 11) {
      options.pop();
      options.push(
        <Option key="more" disabled style={{ textAlign: 'center' }}>
          搜索结果过多，请尝试输入全名或其他信息
        </Option>,
      );
    }
    return options;
  };

  handlePatientChange = (value) => {
    if (!value) {
      this.reset();
    }
  };

  reset() {
    const { form } = this.props;
    form.resetFields();
    this.setState({
      povertyPicList: [],
      authorizationPicList: [],
      insurancePackagePro: null,
      patientDetails: null,
      selPatientReleation: undefined,
      insurancePackageProList: [],
      insuranceSelectedOrder: undefined,
    });
  }

  handlePatientSelect = async (value) => {
    this.reset();
    const optionData = this.patientOptionData && this.patientOptionData.list;
    if (!optionData) return;
    await this.setPromise({
      isgeting: true,
      patientFieldValue: undefined,
      patientDetails: undefined,
      selPatientRelValue: undefined,
      selPatientRelDetails: undefined,
    });
    const index = optionData.findIndex((data) => data.id === value.key);
    if (index !== -1) {
      if (optionData[index].isDisabled) {
        Modal.error({
          title: '该患者已被禁用',
        });
        this.setState({
          patientFieldValue: undefined,
        });
        return;
      }
      if (optionData[index].signStatus === 0) {
        Modal.warning({
          content: '该会员信息缺失，不可进行订药、预约、领取红包等业务。请先完善信息，再进行其他操作。',
          onOk: () => {
            window.location.href = `/customerDetails/${optionData[index].id}/EssentialInfor?editStatus=true`;
          },
          okText: '确定',
        });
        return;
      }
      // 判断购买人是否有手机号
      if (!optionData[index].phone) {
        Modal.warning({
          content: '登记服务需要先补录购买人手机号码！',
          onOk: () => {
            window.location.href = `/customerDetails/${optionData[index].id}/EssentialInfor?editStatus=true`;
          },
          okText: '确定',
        });
        return;
      }

      this.init(optionData[index].id);
      this.patientId = optionData[index].id;
      // const hisCusterList = await api.getHistoryPatientInfo(optionData[index].id)
      // 改成获取家庭成员
      let hisCusterList = await api.getPatientFamilyMember(optionData[index].id);
      hisCusterList = covertFamilyMember2HisMember(hisCusterList);

      this.setState({
        hisCusterList,
      });
      const selectedInsurance =
        this.props.getSelectedInsurance &&
        this.props.getSelectedInsurance.status &&
        this.props.getSelectedInsurance.status.selectedInsurance;
      this.insurancePackageChanged(selectedInsurance);
    } else {
      Modal.error({
        title: '错误提示',
        content: '解析用户信息出错',
      });
    }
  };

  handlePatientRelSelect = (value) => {
    const { patientFieldValue, selPatientReleation, hisCusterList, insurancePackageProList, insuranceSelectedOrder } =
      this.state;
    if (value.key == patientFieldValue.key) {
      message.error('服务对象与购买人的关系与已选关系标签不对应，请重新输入');
      this.setState({
        selPatientRelValue: undefined,
        selPatientRelDetails: undefined,
      });
      return;
    }
    if (hisCusterList && hisCusterList.length && selPatientReleation && value) {
      const is_not_match = hisCusterList.find(
        (i) => i.relationShip != selPatientReleation[0] && i.insuredId == value.key,
      );
      if (is_not_match) {
        message.error('服务对象与购买人的关系与已选关系标签不对应，请重新输入');
        this.setState({
          selPatientRelValue: undefined,
          selPatientRelDetails: undefined,
        });
        return;
      }
    }
    const optionData = this.patientOptionData && this.patientOptionData.list;
    if (!optionData) return;
    const index = optionData.findIndex((data) => data.id === value.key);
    if (index !== -1) {
      if (optionData[index].isDisabled) {
        Modal.error({
          title: '该患者已被禁用',
        });
        this.setState({
          patientFieldValue: undefined,
        });
        return;
      }
      if (optionData[index].signStatus === 0) {
        Modal.warning({
          content: '该会员信息缺失，不可进行订药、预约、领取红包等业务。请先完善信息，再进行其他操作。',
          onOk: () => {
            this.props.router.push(`/customerDetails/${optionData[index].id}?editStatus=true`);
          },
          okText: '确定',
        });
        return;
      }

      // 判断有没有符合用户的服务包档次
      const arr = [];
      const buyed_age = getAgeByBirthday(optionData[index].birthday) || null;

      insurancePackageProList &&
        insurancePackageProList.length &&
        insurancePackageProList.map((i) => {
          if (
            i.maxAge !== undefined &&
            i.maxAge !== null &&
            i.minAge !== undefined &&
            i.minAge !== null &&
            buyed_age >= i.minAge &&
            buyed_age <= i.maxAge
          ) {
            // 同一个子产品，可能服务档次相同，是否有医保或是否贫困不同
            // if (!arr.find((j) => j.gradeName == i.gradeName)) {
            //     arr.push(i)
            // }
            arr.push(i);
          } else if (
            !i.maxAge &&
            !i.minAge &&
            buyed_age - insuranceSelectedOrder.ageRange[0] >= 0 &&
            buyed_age - insuranceSelectedOrder.ageRange[1] <= 0
          ) {
            // if (!arr.find((j) => j.gradeName == i.gradeName)) {
            //     arr.push(i)
            // }
            arr.push(i);
          }
        });

      if (arr.length === 0) {
        message.error('服务对象年龄不符合，无法购买该服务！');
        this.setState({
          selPatientRelValue: undefined,
          selPatientRelDetails: undefined,
        });
      } else {
        this.setRelPatientId(optionData[index].id);
        this.relPatientId = optionData[index].id;
      }
    } else {
      Modal.error({
        title: '错误提示',
        content: '解析用户信息出错',
      });
    }
  };

  setRelPatientId(id) {
    const { getRelPatientAction } = this.props;
    try {
      getRelPatientAction(id);
    } catch (e) {
      console.error(e);
      message.error(e.message);
    }
  }

  handlePreview = async (file) => {
    if (!file.url && !file.preview) {
      file.preview = await getBase64(file.originFileObj);
    }
    const pic = [{ url: file.url || file.preview, alt: file.url || file.preview }];
    Viewer(pic, {
      navbar: false,
      toolbar: false,
      title: false,
    });
  };

  handleChange = ({ file, fileList }, fileParamName) => {
    const { form } = this.props;
    if (fileParamName === 'povertyPicList') {
      this.setState({ povertyPicList: fileList });
    }
    if (fileParamName === 'authorizationPicList') {
      this.setState({ authorizationPicList: fileList });
    }
  };

  handleBeforeUpload = async (file) => {
    this.setState({ preview: undefined });
    //限制图片 格式、size、分辨率
    const isJPG = file.type === 'image/jpeg';
    const isJPEG = file.type === 'image/jpeg';
    const isGIF = file.type === 'image/gif';
    const isPNG = file.type === 'image/png';
    if (!(isJPG || isJPEG || isGIF || isPNG)) {
      Modal.error({
        title: '只能上传JPG 、JPEG 、GIF、 PNG格式的图片~',
      });
      return false;
    }
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
      Modal.error({
        title: '超过2M限制，不允许上传~',
      });
      return false;
    }
    const preview = await getBase64(file);
    this.setState({ preview });
    return true;
  };

  customRequest = (fileData) => {
    api.uploadFile(fileData.data).then(
      (res) => {
        fileData.onSuccess(res);
      },
      (error) => {
        fileData.onError(error);
      },
    );
  };

  uploadFile = (file) => {
    const { preview } = this.state;
    const name = file.name;
    const param = {
      data: preview.substr(preview.indexOf(';base64,') + ';base64,'.length),
      suffix: name.substring(name.lastIndexOf('.') + 1, name.length),
      prefixPath: 'orderinfo',
    };
    return param;
  };

  insurancePackageChanged = async (value) => {
    this.setState({ insurancePackageProList: value.insurancePackagePros || [], insuranceSelectedOrder: value });
  };

  backToPre = () => {
    this.saveSubData();
    this.setState({
      location_href: 1,
      ischange: 1,
    });
  };

  backToSecPre = () => {
    this.setState({
      location_href: 2,
    });
  };

  saveSubData() {
    const { setInsuranceSubmitData, form } = this.props;
    const { getFieldValue } = form;
    const sub_data = {
      patientFieldValue: this.state.patientFieldValue,
      selPatientReleation: this.state.selPatientReleation,
      choseInsurancePatient: this.state.choseInsurancePatient,
      selPatientRelValue: this.state.selPatientRelValue || null,
      guarantee: this.state.guarantee,
      poverty: this.state.poverty,
      insurancePackageProsId: this.state.insurancePackageProsId,
      authorizationPicList: this.state.authorizationPicList,
      payWay: this.state.payWay,
      isInsuranceAccount: this.state.isInsuranceAccount,
      f_patient: this.patientId,
      s_patient:
        this.state.selPatientReleation && this.state.choseInsurancePatient
          ? this.state.selPatientReleation[0] == '1'
            ? this.patientId
            : this.state.choseInsurancePatient[0]
          : null,
      isCash: this.state.isCash,
      is3dCash: this.state.is3dCash,
      isPoint: this.state.isPoint,
      payPoint: this.state.payPoint,
      payInsuranceAccount: this.state.payInsuranceAccount,
      payCash: this.state.payCash,
      pay3dCash: this.state.pay3dCash,
      payPrice: this.state.payPrice,
      buyCode: this.state.buyCode,
      subCode: this.state.subCode,
    };
    setInsuranceSubmitData(sub_data);
  }

  /* 选择对象关系 */
  setBuyPatient = (value) => {
    const { hisCusterList, insuranceSelectedOrder, patientFieldValue, patientDetails, isInsurance } = this.state;

    // console.log('hisCusterList:',hisCusterList)
    // console.log('insuranceSelectedOrder:',insuranceSelectedOrder)
    // console.log('patientFieldValue:',patientFieldValue)
    // console.log('patientDetails:',patientDetails)
    // console.log('isInsurance:',isInsurance)
    // 选择本人
    if (value && value[0] == 1) {
      // 检验是否服务包含有保险 & 是否实名校验
      if (isInsurance && !patientDetails.certification) {
        message.error('服务对象实名认证失败，请确认信息后重新维护该会员信息');
        this.setState({
          selPatientReleation: undefined,
        });
        return;
      }
    }

    // 如服务包不含保险,则校验性别(sex)和出生日期(birthday)
    if (!isInsurance && ((patientDetails.sex !== 1 && patientDetails.sex !== 0) || !patientDetails.birthday)) {
      message.error('请补录性别和出生日期！');
      this.setState({
        selPatientReleation: undefined,
      });
      return;
    }

    if (value && value[0] == 1) {
      // 会员信息是否有身份证信息,如有则使用身份证计算年龄,如没有则使用出生日期计算。
      if (patientDetails.certification && patientDetails.idCard) {
        const t_age = IdCardCheck(patientDetails.idCard);
        if (t_age - insuranceSelectedOrder.ageRange[0] < 0 || t_age - insuranceSelectedOrder.ageRange[1] > 0) {
          message.error('服务对象年龄与服务包限定范围不符，不可购买');
          this.setState({
            selPatientReleation: undefined,
          });
          return;
        }
      } else {
        const t_age = getAgeByBirthday(patientDetails.birthday);
        if (t_age - insuranceSelectedOrder.ageRange[0] < 0 || t_age - insuranceSelectedOrder.ageRange[1] > 0) {
          message.error('服务对象年龄与服务包限定范围不符，不可购买');
          this.setState({
            selPatientReleation: undefined,
          });
          return;
        }
      }
    }

    let now_his_1ist = [];
    if (value && value[0] != '1') {
      now_his_1ist = hisCusterList.filter((i) => i.relationShip == value[0]);
    }
    if (now_his_1ist.length) {
      let selre = [];
      let new_selre = [];
      const agernge = insuranceSelectedOrder && insuranceSelectedOrder.ageRange;
      if (patientFieldValue && value && value[0] != '1') {
        switch (value[0]) {
          case '2':
            new_selre = hisCusterList && hisCusterList.filter((i) => i.relationShip == 2);
            break;
          case '3':
            new_selre = hisCusterList && hisCusterList.filter((i) => i.relationShip == 3);
            break;
          case '4':
            new_selre = hisCusterList && hisCusterList.filter((i) => i.relationShip == 4);
            break;
        }
      }
      console.log('new_selre: ', new_selre);
      new_selre.length &&
        new_selre.map((i) => {
          if (i.insuredIdCard) {
            let age_t;
            if (i.insuredIdCard) {
              age_t = IdCardCheck(i.insuredIdCard);
            } else {
              age_t = getAgeByBirthday(i.birthday);
            }
            if (age_t >= agernge[0] && age_t <= agernge[1]) {
              selre.push(i);
            } else {
              i.isDisabled = true;
              selre.push(i);
            }
          }
        });
      if (selre.length) {
        const selecId = selre.find((i) => !i.isDisabled);
        selecId ? this.selectBuyPatient([`${selecId.insuredId}`]) : null;
        this.setState({
          selPatientReleation: value,
        });
      } else {
        this.setState({
          selPatientRelValue: undefined,
          selPatientRelDetails: undefined,
          selPatientReleation: value,
          choseInsurancePatient: undefined,
        });
      }
    } else {
      this.setState({
        selPatientRelValue: undefined,
        selPatientRelDetails: undefined,
        selPatientReleation: value,
        choseInsurancePatient: undefined,
      });
    }
  };

  /* 社保 */
  setguarantee = (value) => {
    // 有社保和无社保，每个选项都对应多个服务档次。
    const { insurancePackageProList } = this.state;
    const filteredInsurancePackageProList = insurancePackageProList.filter((item) => {
      return item.guarantee == value[0];
    });
    this.setState({
      filteredInsurancePackageProList,
      guarantee: value,
    });

    // const { insuranceSelectedOrder, insurancePackagePro } = this.state;
    // if (value && insurancePackagePro && insurancePackagePro.guarantee !== null && insurancePackagePro.guarantee !== +value[0]) {
    //     const iy = insurancePackagePro.guarantee
    //     Modal.warning({
    //         title: iy === 0 ? '该服务暂不支持社保用户领用' : '该服务暂不支持无社保用户领用',
    //     });
    //     this.setState({
    //         guarantee: undefined,
    //     })
    //     return;
    // }
    // this.setState({
    //     guarantee: value
    // })
  };

  /* 贫困 */
  getInsuracePackage = (value) => {
    const { form, getInsuracePackageAction } = this.props;
    const { insurancePackageProList, nowInsurancePovertyList, insuranceSelectedOrder, insurancePackagePro } =
      this.state;
    console.log('getInsuracePackage:', value, nowInsurancePovertyList, insurancePackagePro);
    if (
      value &&
      insurancePackagePro &&
      insurancePackagePro.poverty !== null &&
      insurancePackagePro.poverty != value[0]
    ) {
      const iy = insurancePackagePro.poverty;
      Modal.warning({
        title: iy == 0 ? '该服务暂不支持贫困用户领用' : '该服务暂不支持非贫困用户领用',
      });
      this.setState({
        poverty: undefined,
      });
      return;
    }
    const { getFieldValue } = form;
    const idCard = getFieldValue('idCard');
    const poverty = value || this.state.poverty;
    const validator = IDValidator;
    const valueStr = String(idCard);
    if (validator.isValid(valueStr) && idCard) {
      getInsuracePackageAction(idCard, null);
    }
    if (value !== undefined) {
      const insurancePackagePro =
        nowInsurancePovertyList &&
        nowInsurancePovertyList.length &&
        nowInsurancePovertyList.find((i) => i.poverty === Number(value[0]));
      this.setState(
        {
          insurancePackagePro,
          poverty: value,
        },
        this.payWayOnChange(null, insurancePackagePro),
      );
    }
  };

  /* 选择档次 */
  insuranceProChanged = async (value) => {
    const { insurancePackageProList, poverty, guarantee } = this.state;
    const insurancePackagePro = insurancePackageProList.find((item) => item.id == value[0]);
    if (
      insurancePackagePro &&
      insurancePackagePro.poverty !== null &&
      poverty !== undefined &&
      poverty[0] != insurancePackagePro.poverty
    ) {
      const iy = insurancePackagePro.poverty;
      Modal.warning({
        title: iy == 0 ? '该服务暂不支持贫困用户领用' : '该服务暂不支持非贫困用户领用',
      });
      this.setState({
        poverty: undefined,
      });
    }
    if (
      insurancePackagePro &&
      insurancePackagePro.guarantee !== null &&
      guarantee !== undefined &&
      guarantee[0] != insurancePackagePro.guarantee
    ) {
      const iy = insurancePackagePro.guarantee;
      Modal.warning({
        title: iy == 0 ? '该服务暂不支持社保用户领用' : '该服务暂不支持无社保用户领用',
      });
      this.setState({
        guarantee: undefined,
      });
    }
    await this.setPromise({ insurancePackagePro, insurancePackageProsId: value, isSubmiting: false });
    if (insurancePackagePro && insurancePackagePro.poverty === null) {
      this.payWayOnChange(null, insurancePackagePro);
    } else {
      const ast = insurancePackageProList.filter((i) => i.gradeName === insurancePackagePro.gradeName);
      this.payWayOnChange(null, insurancePackagePro);
      this.setState({
        nowInsurancePovertyList: ast,
      });
    }
  };

  selectBuyPatient = async (value) => {
    this.setPromise({ isgeting: true });
    const { choseInsurancePatient, insuranceSelectedOrder } = this.state;
    if (!choseInsurancePatient || value[0] != choseInsurancePatient[0]) {
      const patientDetails = await api.getPatient(value[0]);
      const i_age = getAgeByBirthday(patientDetails.birthday);
      if (i_age < insuranceSelectedOrder.ageRange[0] || i_age > insuranceSelectedOrder.ageRange[1]) {
        message.error('服务对象年龄与服务包限定范围不符，不可购买');
        this.setState({
          choseInsurancePatient: undefined,
          isgeting: false,
        });
        return;
      }
      const label = `${patientDetails.name} (${patientDetails.sex ? '男' : '女'}${i_age}) ${
        markPhoneNumber(patientDetails.phone) || ''
      } ${patientDetails.machineNumber || ''} ${patientDetails.address.liveStreet || ''} ${
        patientDetails.hospital.name || ''
      }`;
      const patientFieldValue = {
        key: value[0],
        label,
      };
      this.setState({
        selPatientRelValue: patientFieldValue,
        selPatientRelDetails: patientDetails,
        choseInsurancePatient: value,
        isgeting: false,
      });
    } else if (value && choseInsurancePatient && choseInsurancePatient[0] == value[0]) {
      this.setState({
        selPatientRelValue: undefined,
        selPatientRelDetails: undefined,
        choseInsurancePatient: undefined,
        isgeting: false,
      });
    }
  };

  setBuyCode = (e) => {
    this.setState({
      buyCode: e.target.value.toUpperCase(),
      subCode: e.target.value,
    });
  };

  setRemark = (e) => {
    this.setState({
      remarks: e.target.value,
    });
  };

  submit = async () => {
    // 页面滚动到顶部
    let modalWrapper = document.querySelector('.ant-modal-wrap ');
    modalWrapper && (modalWrapper.scrollTop = 0);
    await this.setPromise({ isSubmiting: true, isSub: true });
    const {
      selPatientReleation,
      poverty,
      guarantee,
      choseInsurancePatient,
      buyCode,
      insuranceSelectedOrder,
      selPatientRelValue,
      subCode,
      patientDetails,
      selectedInfo,
      insurancePackageProsId,
    } = this.state;
    if (!this.patientId) {
      Modal.warning({
        title: '请先选择会员',
      });
      this.setState({ isSubmiting: false });
      return;
    }
    // 当前购买人
    const now_patientDetail = await api.getPatient(this.patientId);
    let new_cityId = null;
    let new_provinceId = null;
    // if(now_patientDetail && now_patientDetail.memberType == 1){ // 保障
    new_cityId = (now_patientDetail.hospital && now_patientDetail.hospital.cityId) || null;
    new_provinceId = (now_patientDetail.hospital && now_patientDetail.hospital.provinceId) || null;
    /* }else{ // 绿a
            new_cityId = now_patientDetail.address.liveCity;
            new_provinceId = now_patientDetail.address.liveProvinces
        } */
    if (
      selectedInfo &&
      selectedInfo.areas &&
      selectedInfo.areas.length &&
      !selectedInfo.areas.find((item) =>
        item.cityId ? item.cityId == new_cityId : item.provincesId ? item.provincesId == new_provinceId : null,
      )
    ) {
      message.error('购买人所在城市与服务包服务区域不符');
      this.setState({ isSubmiting: false, isSub: false });
      return;
    }
    if (subCode) {
      try {
        // 查询激活码是否可用
        const data = await api.checkInsuranceCode(subCode);
        if (!data) {
          message.error('激活码有误，请重新输入');
          this.setState({ isSubmiting: false });
          return;
        } else if (
          data.status == 1 ||
          (data.status == 0 && data.insurancePackageId != insuranceSelectedOrder.packageId)
        ) {
          message.error('激活码已失效或不匹配');
          this.setState({ isSubmiting: false });
          return;
        }
      } catch (err) {
        message.error(err.message);
        this.setState({ isSubmiting: false });
        return;
      }
    }
    // 服务对象
    if (selPatientReleation && selPatientReleation[0] != '1' && !selPatientRelValue) {
      message.error('请选择服务对象');
      return;
    }
    // 选择的服务包
    if (
      (insuranceSelectedOrder && insuranceSelectedOrder.buyWithCode && !buyCode) ||
      !selPatientReleation ||
      (Array.isArray(insuranceSelectedOrder.insurancePackagePros) &&
        insuranceSelectedOrder.insurancePackagePros[0].poverty == 1 &&
        !poverty) ||
      (insuranceSelectedOrder &&
        Array.isArray(insuranceSelectedOrder.insurancePackagePros) &&
        insuranceSelectedOrder.insurancePackagePros[0].guarantee == 1 &&
        !guarantee)
    ) {
      message.error('请完善信息');
      this.setState({ isSubmiting: false });
      return;
    }
    this.props.form.validateFields(async (err, values) => {
      const { povertyPicList } = values;
      const { authorizationPicList } = this.state;
      let picCheck = true;
      if (authorizationPicList && authorizationPicList.length > 0) {
        authorizationPicList.forEach((i) => {
          if (i.status !== 'done') {
            picCheck = false;
          }
        });
      }
      // 贫困非贫困
      if (
        this.state.insurancePackageProList[0].poverty !== null &&
        this.state.poverty &&
        this.state.poverty[0] === '1' &&
        povertyPicList &&
        povertyPicList.length > 0
      ) {
        povertyPicList.forEach((i) => {
          if (i.status !== 'done') {
            picCheck = false;
          }
        });
      }
      if (!picCheck) {
        message.error('请等待图片上传成功后再次提交。');
        this.setState({ isSubmiting: false });
        return;
      }
      let povertyPics;
      povertyPicList
        ? povertyPicList.forEach((i) => {
            povertyPics = povertyPics ? povertyPics + ',' + i.response : i.response;
          })
        : null;
      let authorizationPics;
      authorizationPicList
        ? authorizationPicList.forEach((i) => {
            authorizationPics = authorizationPics ? authorizationPics + ',' + i.response : i.response;
          })
        : null;
      // 服务档次
      if (!insurancePackageProsId) {
        message.error('服务档次不能为空');
        return;
      }
      // 支付
      const payments = [];
      const { payPoint, payInsuranceAccount, payCash, pay3dCash, payPrice } = this.state;
      // 需求2663，不管服务包是不是0元，均需要选择付款方式
      // if (payPrice > 0) {
      if (
        (!this.state.isPoint || this.state.isPoint.length <= 0) &&
        (!this.state.isInsuranceAccount || this.state.isInsuranceAccount.length <= 0) &&
        (!this.state.isCash || this.state.isCash.length <= 0) &&
        (!this.state.is3dCash || this.state.is3dCash.length <= 0)
      ) {
        message.error('请选择支付金额');
        this.setState({ isSubmiting: false });
        return;
      }
      if ((payPoint || 0) + (payInsuranceAccount || 0) + (payCash || pay3dCash || 0) < payPrice) {
        message.error('金额不足支付首月会员服务费，登记失败');
        this.setState({ isSubmiting: false });
        return;
      }
      if (this.state.isPoint && this.state.isPoint.length > 0 && this.state.isPoint[0] === 1) {
        payments.push({ payWay: 'self_pbm_points_deduction', amount: payPoint });
      }
      if (
        this.state.isInsuranceAccount &&
        this.state.isInsuranceAccount.length > 0 &&
        this.state.isInsuranceAccount[0] === 1
      ) {
        payments.push({ payWay: 'self_member_account', amount: payInsuranceAccount });
      }
      if (this.state.isCash && this.state.isCash.length > 0 && this.state.isCash[0] === 1) {
        payments.push({ payWay: 'self_charge', amount: payCash });
      }
      if (this.state.is3dCash && this.state.is3dCash.length > 0 && this.state.is3dCash[0] === 1) {
        payments.push({ payWay: 'self_third', amount: pay3dCash });
      }
      if (!this.state.insurancePackagePro) {
        message.error('服务对象年龄不符合，无法购买该服务！');
        return;
      }
      const submitData = {
        insuredId: this.state.selPatientReleation[0] == '1' ? this.patientId : selPatientRelValue.key,
        insurerId: this.patientId,
        povertyPics,
        authorizationPics,
        insurancePackageProsId: this.state.insurancePackagePro.id,
        payWay: 1,
        code: null,
        usePoints: this.state.isPoint && this.state.isPoint.length > 0 ? this.state.isPoint[0] : 0,
        useAccount:
          this.state.isInsuranceAccount && this.state.isInsuranceAccount.length > 0
            ? this.state.isInsuranceAccount[0]
            : 0,
        payments: JSON.stringify(payments),
        code: subCode,
        relationWithInsurer: Number(this.state.selPatientReleation[0]),
        cid: this.state.channelCode,
        ssOwner: this.state.curSsOwnerId,
        remarks: this.state.remarks,
      };
      // 判断是否45天内是否购药记录开关是否开启
      if (this.state.selectedInfo.validateMemberPickedupOrderCount) {
        // 需要验证
        try {
          const res = await api.pickedupOrderCount(submitData.insuredId, 45);
          if (!res) {
            message.error('该会员健管周期内无购药记录，无法登记会员服务。');
            this.setState({ isSubmiting: false });
            return;
          }
        } catch (err) {
          message.error(err.message);
        }
      }
      // 是否需要问询：true需要，
      if (!this.state.needHealthq) {
        // 添加新弹窗
        this.showSubmitModal(async () => {
          try {
            const data = await api.createPatientInsurance(submitData);
            this.props.savePutData(data);
            this.props.openModal('patientInsuranceResult');
          } catch (err) {
            message.error(err.message);
            this.setState({
              isSubmiting: false,
            });
          }
        });
      } else {
        // 创建待激活的订单
        try {
          let data = {};
          try {
            data = await api.createPatientInsurance(submitData);
          } catch (err) {
            message.error(err.message);
            this.setState({
              isSubmiting: false,
            });
          }
          if (!data.insuranceOrderId) {
            return;
          }
          if (!data.insuranceTeamOrderNo) {
            message.error('订单无保险投保单编号');
            return;
          }
          // 获取订单详情
          const insuranceOrderInfo = await api.getOriginInsuranceOrderInfo(data.insuranceOrderId);
          // 保险公司编码
          let productGradeCode = '';
          let { orderProduct = [] } = insuranceOrderInfo;
          if (orderProduct) {
            for (let i = 0; i < orderProduct.length; i++) {
              if (orderProduct[i].productType === 1) {
                productGradeCode = orderProduct[i].productGradeCode;
                break;
              }
            }
          }
          // 众惠问询  inquiryType 0本地 1跳转到第三方
          if (this.state.inquiryData && this.state.inquiryData.inquiryType === 1) {
            const { inquiryData } = this.state;
            const href =
              location.origin +
              '/patientInsurance' +
              `?orderId=${data.insuranceOrderNo}&teamOrderNo=${data.insuranceTeamOrderNo}`;
            const url = `${inquiryData.inquiryUrl}?name=${data.insuredName}&cardNo=${data.insuredIdCard}&orderId=${
              data.insuranceTeamOrderNo
            }&productGradeCode=${productGradeCode}&returnUrl=${encodeURIComponent(href)}`;
            window.location.href = url;
            return;
          } else {
            this.setState({
              orderInfo: data,
            });
            this.inquiryFn(insuranceSelectedOrder, data);
          }
        } catch (error) {
          message.error(typeof error === 'object' && error.message);
          this.setState({
            isSubmiting: true,
          });
        }
      }
    });
  };

  // 第三方问询
  inquiryFn = async (packInfo, data) => {
    const inquiryLink = `${envType().inquiryLink}?e=${envType().e}&packageId=${packInfo.packageId}&teamOrderNo=${
      data.insuranceTeamOrderNo
    }&token=${getCookie('token')}`;
    const l = (document.body.clientWidth - 540) / 2;
    win = window.open(
      inquiryLink,
      '健康问询',
      `width=540,height=720,left=${l},top=${60}menubar=no,toolbar=no,location=no,directories=no,status=no`,
    );
  };

  // postMessage消息处理
  postMsg = async (event) => {
    const { data } = event;
    win.close();
    win = null;

    await api.insureInsureOrderNoV2(this.state.orderInfo.insuranceTeamOrderNo, data);
    this.props.savePutData(this.state.orderInfo);
    console.log('orderInfo: ', this.state.orderInfo);
    // this.props.savePutData({
    //   insuranceOrderId: 1794,
    //   insuranceOrderNo: 'vpo2021102500001770',
    //   insuranceTeamOrderNo: '182021102500962',
    //   insuredName: '李易峰',
    //   insuredIdCard: '42098419550910293X',
    //   insuredPhone: '13601265489',
    //   relationWithInsurer: 1,
    //   insurerName: '李易峰',
    //   insurerIdCard: '42098419550910293X',
    //   insurerPhone: '13601265489',
    //   insurancePackageName: '众惠团险问询测试',
    //   insuranceGradeName: '标准版',
    //   payWay: 1,
    //   amount: 0,
    //   paidAmount: 0,
    //   insuranceOrderBillId: null,
    //   payOrderId: null,
    //   payInfo: null,
    //   transDate: null,
    //   payStatus: 1,
    //   orderStatus: 3,
    //   needMedicationData: false,
    //   insuredId: null,
    //   projectType: 6,
    //   packageCode: 'IP0728',
    //   aftersalesNote: null,
    //   aftersalesImg: null,
    //   examineInsuranceDate: null,
    // });

    this.props.openModal('patientInsuranceResult');
  };

  gotoNext = async () => {
    // 页面滚动到顶部
    let modalWrapper = document.querySelector('.ant-modal-wrap ');
    modalWrapper && (modalWrapper.scrollTop = 0);

    const { selectedInfo, selectedVal, selectedInsuranceId, ischange } = this.state;
    const { setInsuraceSelected, setSelectedInsuranceId, setInsuranceSubmitData } = this.props;
    if (!selectedVal) {
      message.error('请选择服务包');
      return;
    }

    /**
         * selectedInfo: insuranceIndexPackageList[value[0]],
            selectedVal: value,
            isSubmiting: false,
            ischange: selectedVal && value && selectedVal[0] == value[0] ? 1 : 2,
            releation_ship_list,
            selectedVal2: insuranceIndexPackageList[value[0]].insurancePackageName
         */

    setInsuraceSelected(selectedInfo);
    setSelectedInsuranceId(selectedVal);
    // const diseasesList = await api.getInsuranceDiseases(selectedInfo.packageId);
    // 根据服务包查询是否有问询
    const inquiryData = await api.getInsuranceInquiries(selectedInfo.packageId);
    // 获取问询，需求2006，更换问询的接口，1，众惠   2   安心  3.众惠防贫

    if (inquiryData && inquiryData.needInquiry) {
      // inquiryData.inquiryType === 1 ? 第三方问询 : 本地问询
      if (inquiryData.inquiryType === 1) {
        this.setState({
          inquiryData,
        });
      } else {
        let inquiryId = +inquiryData.inquiryId;
        if (inquiryId === 1 || inquiryId === 4) {
          // 需求2017，众惠问询进行升级
          inquiryId = 6;
        } else if (inquiryId === 2) {
          inquiryId = 4;
        } else if (inquiryId === 3) {
          inquiryId = 5;
        } else if (inquiryId === 7) {
          // 大家保险问询
        }

        // inquiryId为456的问询，存放在mongodb里，7为新加的大家保险的问询,存放在js文件里
        // let diseasesList = []
        // if(inquiryId === 4 ||inquiryId === 5 ||inquiryId === 6){
        //     diseasesList = await api.getEnquiryList(inquiryId)
        // }else{
        //     diseasesList = jsInquiryData[inquiryId].data
        // }
        let diseasesList = await api.getEnquiryList(inquiryId);
        this.setState({
          diseasesList: diseasesList,
          inquiryId,
        });
      }
      this.setState({
        location_href: 2,
        needHealthq: true,
      });
    } else {
      this.setState({
        location_href: 2,
        diseasesList: [],
        needHealthq: false,
      });
    }
    if (ischange == 2) {
      this.resetData();
    }
  };

  resetData() {
    this.setState({
      patientFieldValue: undefined,
      selPatientReleation: undefined,
      selPatientRelValue: undefined,
      choseInsurancePatient: undefined,
      guarantee: undefined,
      poverty: undefined,
      insurancePackageProsId: undefined,
      hisCusterList: undefined,
      isCash: undefined,
      is3dCash: undefined,
      isPoint: undefined,
      isInsuranceAccount: undefined,
      patientDetails: undefined,
      selPatientRelDetails: undefined,
      payPoint: undefined,
      payInsuranceAccount: undefined,
      payCash: undefined,
      pay3dCash: undefined,
      payPrice: undefined,
      insuranceSelectedOrder: undefined,
      buyCode: undefined,
      povertyPicList: [],
      authorizationPicList: [],
      insurancePackageProList: [],
      filteredInsurancePackageProList: [], // 用户选择社保和非社保之后，得到了符合条件的子产品列表
      insurancePackagePro: undefined,
      insuranceAccountInfo: undefined,
      isProhibit: false,
      povertyValidate: undefined,
      payWay: ['1'],
      q_list: {},
      isSubmiting: false,
      subCode: null,
    });
  }

  getQueInfo() {
    const { q_list, diseasesList } = this.state;
    const c = [];
    diseasesList.map((i, index) => {
      if (q_list[i.id] && q_list[i.id].length) {
        q_list[i.id].map((item) => {
          i.answer.map((j, j_index) => {
            const iu = {};
            const iu_list = [];
            if (item == j_index) {
              iu_list.push(j);
              iu.id = i.id;
              iu.question = i.question;
              iu.answerTitle = i.answerTitle;
              iu.answer = iu_list;
              iu.key = i.key;
              c.push(iu);
            }
          });
        });
      }
    });
    var map = {},
      dest = [];
    for (var i = 0; i < c.length; i++) {
      var ai = c[i];
      if (!map[ai.id]) {
        dest.push({
          id: ai.id,
          answer: ai.answer,
          question: ai.question,
          answerTitle: ai.answerTitle,
          key: ai.key,
        });
        map[ai.id] = ai;
      } else {
        for (var j = 0; j < dest.length; j++) {
          var dj = dest[j];
          if (dj.id == ai.id) {
            dj.answer = [...dj.answer, ...ai.answer];
            break;
          }
        }
      }
    }
    var messageInfo = '';
    const iu = [];
    dest.map((i) => {
      if (i.answer && i.answer.length && i.answer[0].value !== 'NONE') {
        iu.push(i);
      }
    });
    iu.map((i) => {
      messageInfo += i.question;
      i.answer.map((j, index) => {
        if (index != i.answer.length - 1) {
          messageInfo = messageInfo + j.name + ',';
        } else {
          messageInfo = messageInfo + j.name + ';';
        }
      });
    });
    return { messageInfo, nesData: iu };
  }

  setqa(value, index, item) {
    const { diseasesList, q_list } = this.state;
    if (value !== undefined) {
      const isNo = value && value.find((items) => diseasesList[index].answer[items].value === 'NONE');
      if (isNo !== undefined && value.indexOf(String(isNo)) === value.length - 1) {
        q_list[item.id] = [`${isNo}`];
        this.setState({
          q_list,
        });
      } else if (isNo !== undefined && value.indexOf(String(isNo)) !== value.length - 1) {
        const itemIndex = value.indexOf(String(isNo));
        value.splice(itemIndex, 1);
        q_list[item.id] = value;
        this.setState({
          q_list,
        });
      } else {
        q_list[item.id] = value;
        this.setState({
          q_list,
        });
      }
    } else {
      q_list[item.id] = [];
      this.setState({
        q_list,
      });
    }
  }

  getAnsInfo(data, item) {
    const { diseasesList } = this.state;
    if (data.length) {
      const q_info = diseasesList[item];
      const i_num = +data[0];
      if (data.length === 1 && q_info && q_info.answer[i_num] && q_info.answer[i_num].value === 'NONE') {
        return '';
      } else if (data.length && q_info && q_info.answer[i_num] && q_info.answer[i_num].value !== 'NONE') {
        const arr = [];
        let disInfo = '';
        // const quesTitle = <div>{q_info.answerTitle ? q_info.answerTitle : ''}</div>;
        // console.log('quesTitle:',quesTitle)
        data.map((i) =>
          q_info.answer.map((j, index) => {
            if (index == i) {
              arr.push(j);
            }
          }),
        );
        // arr.map((item, index) => {
        //     const ij = <div>{item.result ? item.result : item.name + '；'}</div>
        //     disInfo = disInfo + ij
        // })
        // console.log('disInfo:',disInfo)
        // return quesTitle + disInfo
        // return ''
        return (
          <div>
            <div>{q_info.answerTitle ? q_info.answerTitle : ''}</div>
            <div>
              {arr.map((item, index) => {
                return <div>{item.result ? item.result : item.name + '；'}</div>;
              })}
            </div>
          </div>
        );
      } else {
        return '';
      }
    } else {
      return '';
    }
  }
  showSubmitModal = (okCallback, cancelCallback) => {
    let that = this;
    Modal.confirm({
      title: '',
      content: ` 确认激活【${this.state.selectedVal2}】会员服务包？`,
      onOk() {
        okCallback();
      },
      onCancel() {
        that.setState({
          iscomplete: false,
          isSubmiting: false,
        });
        cancelCallback && cancelCallback();
      },
    });
  };
  thirdSubmit = async () => {
    // 页面滚动到顶部
    let modalWrapper = document.querySelector('.ant-modal-wrap ');
    modalWrapper && (modalWrapper.scrollTop = 0);

    const { diseasesList: diseasesListStr, getPreSubmitData, q_list, nesData, iscomplete, selectedInfo } = this.state;
    const diseasesList = JSON.parse(JSON.stringify(diseasesListStr));
    await this.setPromise({ iscomplete: true });
    const afg = [];
    for (let i in q_list) {
      if (q_list[i] && q_list[i].length) {
        afg.push(i);
      }
    }
    if (afg.length !== diseasesList.length) {
      message.error('请回答完所有问题');
      this.setState({
        iscomplete: false,
      });
      return;
    }
    const index_forbidden = diseasesList.filter((i) => i.type === 'forbidden');
    let cansub = [];
    if (index_forbidden && index_forbidden.length) {
      index_forbidden.map((i) => {
        const inum = Number(q_list[i.id][0]);
        if (q_list[i.id].length && i.answer[inum].value !== 'NONE') {
          cansub.push(1);
        }
      });
    }
    if (cansub.length > 0) {
      message.error('有禁止的选项');
      this.setState({
        iscomplete: false,
      });
      return;
    }
    const messageInfo = this.getQueInfo();

    let forbiddenList2 = diseasesList.filter((item) => {
      return item.type === 'forbidden';
    });
    let warningList2 = diseasesList.filter((item) => {
      return item.type === 'warning';
    });

    let questionnaire = [];

    forbiddenList2.forEach((item) => {
      // let answerIndex = q_list[item.id][0]
      // // 0 为否  1 为是
      // let answer = item.answer[answerIndex].value ==='NONE' ? '0' : '1'
      // console.log('answerIndex:',answerIndex)
      // console.log('answer:',answer)
      let obj = {
        code: item.server,
        answer: '0',
        question: item.question,
      };
      questionnaire.push(obj);
    });
    warningList2.forEach((item) => {
      let answerIndex = q_list[item.id];
      item.answer.forEach((el, index) => {
        // TODO: 是否可以这样判断？
        if (el.name !== '以上皆无') {
          el.question = el.name;
          delete el.name;
          delete el.result;
          el.answer = answerIndex.includes(String(index)) ? '1' : '0';
          questionnaire.push(el);
        }
      });
    });

    const subdata = {
      ...getPreSubmitData,
      diseasesInfo: messageInfo.messageInfo,
      questionnaire,
    };

    const diseaseInfo = {
      diseasesList,
      q_list,
    };
    const now_patientDetail = await api.getPatient(this.patientId);
    let new_cityId = null;
    let new_provinceId = null;
    // if(now_patientDetail && now_patientDetail.memberType == 1){ // 保障
    new_cityId = (now_patientDetail.hospital && now_patientDetail.hospital.cityId) || null;
    new_provinceId = (now_patientDetail.hospital && now_patientDetail.hospital.provinceId) || null;
    /* }else{ // 绿a
            new_cityId = now_patientDetail.address.liveCity;
            new_provinceId = now_patientDetail.address.liveProvinces
        } */
    if (
      selectedInfo &&
      selectedInfo.areas &&
      selectedInfo.areas.length &&
      !selectedInfo.areas.find((item) =>
        item.cityId ? item.cityId == new_cityId : item.provincesId ? item.provincesId == new_provinceId : null,
      )
    ) {
      message.error('购买人所在城市与服务包服务区域不符');
      this.setState({ iscomplete: false });
      return;
    }

    const tips = [];
    messageInfo.nesData.forEach((item) => {
      return item.answer.forEach((i) => tips.push(i.result || i.name));
    });

    let waiver = '';
    if (tips.length) {
      let temp = '保险公司将不承担被保险人以下几种重大疾病相关的治疗费用给付责任：' + tips.join('、');
      waiver = temp.replace(/；/g, '') + '。';
    } else {
      waiver = '';
    }

    // 初始化大家保险同意书
    let djInnerHtml = this.renderDjAgreement();
    // setTimeout(()=>{
    //     let djInnerHtml = document.querySelector('.dj-agree')
    //     console.log('djInnerHtml:',djInnerHtml)
    // },1000)
    // let djInnerHtml2 = document.querySelector('.dj-agree')
    // console.log('djInnerHtml:',djInnerHtml)

    // console.log('diseasesList:',diseasesList)
    // console.log('q_list:',q_list)
    // 确定激活
    try {
      this.props.saveDiseaseInfo(messageInfo.nesData);
      const data = await api.insureInsureOrderNoV2(getPreSubmitData.insuranceTeamOrderNo, {
        waiver,
        results: questionnaire,
        insuranceConfirmationSpec: djInnerHtml,
      });
      this.setState({
        iscomplete: false,
      });
      if (data) {
        this.props.openModal('patientInsuranceResult');
      } else {
        this.props.openModal('patientInsuranceError');
      }
    } catch (err) {
      console.log(err);
      message.error(err.message);
      this.setState({
        iscomplete: false,
      });
    }
  };

  renderError() {
    return <div style={{ color: 'red', position: 'relative', top: '-20px', left: '144px' }}>不能为空</div>;
  }

  renderThirdError(index) {
    const { diseasesList, q_list } = this.state;
    const index_forbidden = diseasesList.filter((i) => i.type === 'forbidden');
    let cansub = true;
    if (index_forbidden && index_forbidden.length) {
      index_forbidden.map((i, item) => {
        if (q_list[i.id]) {
          const inum = Number(q_list[i.id][0]);
          if (q_list[i.id].length && i.answer[inum] && i.answer[inum].value !== 'NONE') {
            cansub = false;
          }
        }
      });
    }
    if (cansub) {
      return null;
    } else {
      return <div style={{ color: 'red' }}>提示：不可为该服务对象选购此服务</div>;
    }
  }

  renderWarn(data, index) {
    const { diseasesList, q_list } = this.state;
    const index_forbidden = diseasesList.filter((i) => i.type === 'warning');
    let cansub = true;
    if (index_forbidden && index_forbidden.length && q_list) {
      index_forbidden.map((i) => {
        if (q_list[i.id]) {
          const inum = Number(q_list[i.id][0]);
          if (q_list[i.id].length && i.answer[inum].value !== 'NONE') {
            cansub = false;
          }
        }
      });
    }
    const message_info = this.getAnsInfo(data, index);
    if (cansub) {
      return null;
    }
    return (
      <div key={index} style={{ color: '#faad14' }}>
        {message_info}
      </div>
    );
  }
  gotoPrev = () => {
    let location_href = this.state.location_href;
    this.setState({
      location_href: location_href - 1,
    });
  };

  renderFooter() {
    const { location_href, isSubmiting, needHealthq, iscomplete, q_list, diseasesList } = this.state;
    if (location_href === 0) {
      return (
        <Row>
          <Button onClick={this.gotoSelectPkg} type="primary">
            下一步
          </Button>
        </Row>
      );
    }
    if (location_href === 1) {
      return (
        <Row>
          <Button onClick={this.gotoPrev} type="primary">
            上一步
          </Button>
          <Button onClick={this.gotoNext} type="primary">
            下一步
          </Button>
        </Row>
      );
    } else if (location_href === 2) {
      if (diseasesList && diseasesList.length && q_list) {
      }
      return (
        <Row>
          <Button onClick={this.backToPre} type="primary">
            上一步
          </Button>
          <Button
            onClick={this.submit}
            type="primary"
            loading={needHealthq ? false : isSubmiting}
            disabled={needHealthq ? false : isSubmiting}
          >
            提交
          </Button>
        </Row>
      );
    } else if (location_href === 3) {
      let cansub = false;
      diseasesList &&
        diseasesList.map((i) => {
          if (!q_list[i.id] || !q_list[i.id].length) {
            cansub = true;
          }
          if (
            q_list[i.id] &&
            i.type === 'forbidden' &&
            (q_list[i.id].length > 1 ||
              (q_list[i.id].length == 1 && i.answer[Number(q_list[i.id][0])].value !== 'NONE'))
          ) {
            cansub = true;
          }
        });
      return (
        <Row>
          <Button onClick={this.backToSecPre} type="primary">
            上一步
          </Button>
          <Button onClick={this.thirdSubmit} loading={iscomplete} disabled={cansub} type="primary">
            提交
          </Button>
        </Row>
      );
    }
  }
  insuranceIndexPackageChanged = async (value) => {
    const { insuranceIndexPackageList, selectedVal } = this.state;
    const ast = insuranceIndexPackageList[value[0]];
    const releation_ship = ast ? ast.relationDicts : [];
    let releation_ship_list = [];
    if (releation_ship && releation_ship.length) {
      releation_ship.map((i) => {
        const gf = {
          id: i.value.toString(),
          name: i.label,
        };
        releation_ship_list.push(gf);
      });
    }
    this.setState(
      {
        selectedInfo: insuranceIndexPackageList[value[0]],
        selectedVal: value,
        isSubmiting: false,
        ischange: selectedVal && value && selectedVal[0] == value[0] ? 1 : 2,
        releation_ship_list,
      },
      () => {},
    );
  };
  //搜索时
  handleBtnSearch = (value) => {
    // 清空之前的选择
    this.insurancePackageChanged({});
    //深拷贝一组数据
    let list = cloneDeep(this.state.insuranceIndexPackageList2) || [];
    let filterArr = [];
    //判断时数组还是字符
    if (value.substring(0, 2) === 'IP') {
      filterArr = list.filter((item) => item.packageCode === value);
    } else {
      filterArr = list.filter((item) => item.insurancePackageName.indexOf(value) >= 0);
    }
    // if(isNaN(Number(value))){
    //     //非数字匹配名字
    // filterArr=list.filter(item=>(item.insurancePackageName).indexOf(value)>=0)
    // }else{
    //     //数字精确匹配
    //     filterArr=list.filter(item=>item.packageCode.toString()===value)
    // }
    this.setState({
      insuranceIndexPackageList: filterArr,
    });
    if (!value) {
      this.setState({
        insuranceIndexPackageList: this.state.insuranceIndexPackageList2,
      });
    }
  };
  //改变时
  handleBtnChange = (value) => {
    value = [`${value}`];
    const { insuranceIndexPackageList, selectedVal } = this.state;
    const ast = insuranceIndexPackageList[value[0]];
    const releation_ship = ast ? ast.relationDicts : [];
    let releation_ship_list = [];
    if (releation_ship && releation_ship.length) {
      releation_ship.map((i) => {
        const gf = {
          id: i.value.toString(),
          name: i.label,
        };
        releation_ship_list.push(gf);
      });
    }

    this.setState({
      selectedInfo: insuranceIndexPackageList[value[0]],
      selectedVal: value,
      isSubmiting: false,
      ischange: selectedVal && value && selectedVal[0] == value[0] ? 1 : 2,
      releation_ship_list,
      selectedVal2: insuranceIndexPackageList[value[0]].insurancePackageName,
    });
  };
  // 复原服务包数据
  resetMemberPackage = () => {
    this.setState({
      selectedInfo: null,
      selectedVal: '',
      isSubmiting: false,
      ischange: 1,
      releation_ship_list: [],
      selectedVal2: '',
    });
  };

  // 服务专员变化时调用的函数
  ssOwnerChange = (value) => {
    // 当前购买人
    let ssOwners = this.state.ssOwners;
    let curSsOwner = ssOwners.find((item) => {
      return item.userId === value;
    });
    const { setFieldsValue } = this.props.form;
    setFieldsValue({
      ssOwner: value,
    });
    this.setState({
      channelCode: curSsOwner.channelCode,
      curSsOwnerId: value,
    });

    // 服务包初始化为空
    this.resetMemberPackage();
  };

  // 进入选择服务包页面
  gotoSelectPkg = async () => {
    // 获取该服务专员下的服务包
    let curSsOwnerId = this.state.curSsOwnerId;
    let insuranceIndexPackageList = await api.getUserInsuranceListWidthFalse(curSsOwnerId);
    this.setState({
      location_href: 1,
      insuranceIndexPackageList: insuranceIndexPackageList,
      insuranceIndexPackageList2: insuranceIndexPackageList,
    });
  };
  // 渲染选择服务专员的页面
  renderSsownerPage = () => {
    let { userInfo, ssOwners, curSsOwnerId } = this.state;
    const { form } = this.props;
    const { getFieldDecorator, getFieldValue } = form;
    return (
      <div className="ssowner" id="">
        <Form id="ssownerForm">
          <FormItem label="销售人" required {...formItemStyle}>
            {getFieldDecorator('ssOwner', {
              initialValue: curSsOwnerId || userInfo.user,
              rules: { required: true, message: '不能为空' },
            })(
              <Select
                placeholder="请选择销售人"
                showSearch
                defaultActiveFirstOption={false}
                optionFilterProp="children"
                onChange={this.ssOwnerChange}
                notFoundContent=""
              >
                {ssOwners.map((row) => (
                  <Option key={row.userId} value={row.userId}>
                    {row.name + `(${row.channelCode})` + `-${row.companyName}`}
                  </Option>
                ))}
              </Select>,
            )}
          </FormItem>
        </Form>
      </div>
    );
  };

  renderIndexPage() {
    const { insuranceIndexPackageList, isSubmiting, selectedInfo, loading, selectedVal, selectedVal2 } = this.state;
    const { form } = this.props;
    const { getFieldDecorator, getFieldValue } = form;
    const title = '登记会员服务';
    const insurances = selectedInfo
      ? selectedInfo.insurancePackagePros &&
        selectedInfo.insurancePackagePros[0].products &&
        selectedInfo.insurancePackagePros[0].products.map((item) => item.productName)
      : [];
    const options =
      insuranceIndexPackageList &&
      insuranceIndexPackageList.map((item, index) => {
        return (
          <Option key={item.packageId} value={String(index)}>
            {item.insurancePackageName}({item.packageCode})
          </Option>
        );
      });
    return (
      <Spin spinning={loading}>
        <Form id="creatInsuranceOrderIndexPage">
          <Row>
            <Form.Item label="会员服务包" required {...formItemStyle}>
              <Select
                showSearch
                value={selectedVal2}
                placeholder="请输入产品编码/服务包产品名称"
                defaultActiveFirstOption={false}
                showArrow={false}
                filterOption={false}
                onSearch={debounce(this.handleBtnSearch, 200)}
                onChange={this.handleBtnChange}
                notFoundContent={null}
              >
                {options}
              </Select>
              {/* <SmartSelectBox
                                        editStatus
                                        notEditableOnly={false}
                                        onChange={this.insuranceIndexPackageChanged}
                                        buttonOptions={
                                            insuranceIndexPackageList && insuranceIndexPackageList.map((item, index) => {
                                                return {
                                                    id: String(index),
                                                    name: item.insurancePackageName,
                                                }
                                            })
                                        }
                                        getPopupContainer={() => document.getElementById('creatPatientInsurance')}
                                        value = {selectedVal}
                                    /> */}
            </Form.Item>
          </Row>
          <Row>
            <Form.Item label="年龄范围：" {...formItemStyle}>
              {getFieldDecorator('insuranceAge', {
                rules: [],
              })(
                <span>
                  {selectedInfo && selectedInfo.ageRange
                    ? `${selectedInfo.ageRange[0]}-${selectedInfo.ageRange[1]}`
                    : null}
                </span>,
              )}
            </Form.Item>
          </Row>
          <Row>
            <Form.Item label="服务期限：" {...formItemStyle}>
              {getFieldDecorator('insuranceAge', {
                rules: [],
              })(<span>{loading ? '' : selectedInfo && selectedInfo.packageValidPeriod}</span>)}
            </Form.Item>
          </Row>
          <Row>
            <Form.Item label="服务产品：" {...formItemStyle}>
              {getFieldDecorator('insuranceAge', {
                rules: [],
              })(
                <div>
                  {Array.isArray(insurances) &&
                    insurances.map((i, index) => {
                      return <p key={index}>{i}</p>;
                    })}
                </div>,
              )}
            </Form.Item>
          </Row>
          <Row>
            <Form.Item label="服务城市：" {...formItemStyle}>
              {getFieldDecorator('insuranceAge', {
                rules: [],
              })(
                <div>
                  {selectedInfo && selectedInfo.areas && selectedInfo.areas.length
                    ? selectedInfo.areas.map((i, index) => {
                        return (
                          <span style={{ marginRight: '5px' }} key={index}>
                            {i.cityName || i.provincesName || ''}
                          </span>
                        );
                      })
                    : null}
                </div>,
              )}
            </Form.Item>
          </Row>
          <Row>
            <Form.Item label="备注：" {...formItemStyle}>
              {getFieldDecorator('insuranceAge', {
                rules: [],
              })(<div>{selectedInfo && selectedInfo.remarks ? selectedInfo.remarks : ''}</div>)}
            </Form.Item>
          </Row>
        </Form>
      </Spin>
    );
  }
  //设置渠道码
  setChooseCode = (value) => {
    this.setState({ channelCode: value });
  };
  handleSearch = async (value) => {
    const { channelCodeData } = this.state;
    if (value) {
      this.setState({
        loading: true,
      });
      //过滤出符合得值
      const data2 = channelCodeData.filter((item) => item.text.indexOf(value) >= 0);
      this.setState({
        channelCodeData: [...data2],
        loading: false,
      });
    } else {
      //获取当前用户的渠道码
      let user = JSON.parse(window.localStorage.getItem('userInfo'));
      let data = await api.getChannelCode({});

      const data2 = data.map((el) => ({
        text: `${el.name}(${el.channelCode})-${el.companyName}`,
        value: `${el.channelCode}`,
      }));
      //设置默认
      const moren = data.filter((item) => item.userId == user.user);
      this.setState({
        channelCodeData: [...data2],
      });
    }
  };
  setCodeList() {
    const { loading, channelCodeData, channelCode, channelisCodeDis } = this.state;
    const options =
      channelCodeData && channelCodeData.length > 0
        ? channelCodeData.map((d) => (
            <Option key={d.value} value={d.value}>
              {d.text}
            </Option>
          ))
        : null;
    return (
      <Select
        showSearch
        value={channelCode}
        placeholder={null}
        style={{ width: '100%' }}
        defaultActiveFirstOption={false}
        showArrow={false}
        filterOption={false}
        onSearch={debounce(this.handleSearch, 500)}
        onChange={this.setChooseCode}
        notFoundContent={loading ? <Spin size="small" /> : null}
        disabled={channelisCodeDis}
      >
        {options}
      </Select>
    );
  }

  renderSecondPage() {
    const {
      patientFieldValue,
      insurancePackageProList,
      filteredInsurancePackageProList,
      patientDetails,
      insuranceAccountInfo,
      insurancePackagePro,
      payPoint,
      payInsuranceAccount,
      payCash,
      pay3dCash,
      totalPrice,
      payPrice,
      diseaseList,
      isSubmiting,
      healthInquiryResults,
      isProhibit,
      healthInquiryValidate,
      povertyValidate,
      selPatientRelValue,
      selPatientRelDetails,
      hisCusterList,
      isgeting,
      selPatientReleation,
      choseInsurancePatient,
      guarantee,
      poverty,
      insurancePackageProsId,
      payWay,
      isPoint,
      isInsuranceAccount,
      isCash,
      is3dCash,
      isSub,
      insuranceSelectedOrder,
      buyCode,
      needHealthq,
      releation_ship_list,
      channelCode,
      curSsOwnerId,
    } = this.state;

    const { form } = this.props;
    const { getFieldDecorator, getFieldValue } = form;
    const title = '激活服务';
    const uploadButton = (
      <div>
        <div className="ant-upload-text">点击上传</div>
        <div className="ant-upload-text">（仅限图片）</div>
      </div>
    );
    const arr = [];

    const buyed_age = selPatientReleation
      ? selPatientReleation[0] == '1'
        ? getAgeByBirthday(patientDetails.birthday)
        : selPatientRelDetails
        ? getAgeByBirthday(selPatientRelDetails.birthday)
        : null
      : null;

    // 存在医保和非医保 targetInsurancePackageProList 为选过医保后，过滤剩余的服务包
    let targetInsurancePackageProList =
      insurancePackageProList.length > 0 &&
      insurancePackageProList[0].guarantee !== null &&
      insurancePackageProList[0].guarantee !== undefined
        ? filteredInsurancePackageProList
        : insurancePackageProList;
    targetInsurancePackageProList &&
      targetInsurancePackageProList.length &&
      targetInsurancePackageProList.map((i) => {
        if (i.maxAge && i.minAge !== undefined && i.minAge !== null && buyed_age >= i.minAge && buyed_age <= i.maxAge) {
          // 同一个子产品，可能服务档次相同，是否有医保或是否贫困不同
          // if (!arr.find((j) => j.gradeName == i.gradeName)) {
          //     arr.push(i)
          // }
          arr.push(i);
        } else if (
          i.maxAge === null &&
          i.minAge === null &&
          buyed_age - insuranceSelectedOrder.ageRange[0] >= 0 &&
          buyed_age - insuranceSelectedOrder.ageRange[1] <= 0
        ) {
          // if (!arr.find((j) => j.gradeName == i.gradeName)) {
          //     arr.push(i)
          // }
          arr.push(i);
        }
      });

    // console.log('buyed_age:',buyed_age)
    // console.log('arr:',arr)
    // message.error('arr')
    // if(buyed_age!==null && buyed_age!==undefined && arr.length===0){
    //     message.error('没有符合该用户条件的服务包档次')
    // }

    let selre = [];
    let new_selre = [];
    const agernge = insuranceSelectedOrder && insuranceSelectedOrder.ageRange;
    if (patientFieldValue && selPatientReleation && selPatientReleation[0] != '1') {
      switch (selPatientReleation[0]) {
        case '2':
          new_selre = hisCusterList && hisCusterList.filter((i) => i.relationShip == 2);
          break;
        case '3':
          new_selre = hisCusterList && hisCusterList.filter((i) => i.relationShip == 3);
          break;
        case '4':
          new_selre = hisCusterList && hisCusterList.filter((i) => i.relationShip == 4);
          break;
      }
    }
    new_selre.length &&
      new_selre.map((i) => {
        let age_t;
        if (i.insuredIdCard) {
          age_t = IdCardCheck(i.insuredIdCard);
        } else {
          age_t = getAgeByBirthday(i.birthday);
        }
        if (age_t >= agernge[0] && age_t <= agernge[1]) {
          selre.push(i);
        } else {
          i.isDisabled = true;
          selre.push(i);
        }
      });
    return (
      <Spin spinning={isgeting}>
        <Form id="creatPatientInsurance">
          {insuranceSelectedOrder && insuranceSelectedOrder.buyWithCode ? (
            <Row>
              <Form.Item label="激活码" required {...formItemStyle} {...povertyValidate}>
                <Input onChange={this.setBuyCode} value={buyCode} />
              </Form.Item>
              {isSub && !buyCode ? this.renderError() : null}
            </Row>
          ) : null}
          <Row>
            <Form.Item field="patientId" label="购买人" required {...formItemStyle}>
              <div>
                <SmartSelectSingleAsync
                  {...mapPropsToFormItems}
                  className="-x-id-search"
                  placeholder="输入会员姓名/身份证号/手机号/其他联系方式"
                  showSearch
                  value={patientFieldValue}
                  filterOption={false}
                  delay
                  asyncResultId="addMedicineRegister.ChoosePatient"
                  asyncRequestFuncName="searchPatient"
                  asyncRequestParams={{ ssOwner: curSsOwnerId }}
                  onChange={this.handlePatientChange}
                  onSelect={this.handlePatientSelect}
                  cleanOptionsOnBlur
                  getPopupContainer={() => document.getElementById('creatPatientInsurance')}
                  asyncMapResultToState={(data, params) => (params.keyWord ? data : undefined)}
                  mapDataToOption={this.mapDataToOption}
                />
                <div style={{ maxHeight: '30px', paddingTop: '10px' }}>
                  {patientDetails && patientDetails.gradeIcon ? (
                    <img src={patientDetails.gradeIcon} style={{ height: '30px' }} />
                  ) : null}
                  {patientDetails && patientDetails.certification ? (
                    <img src={PatientcertIficationImg} style={{ height: '30px' }} />
                  ) : null}
                </div>
              </div>
            </Form.Item>
          </Row>
          {patientFieldValue ? (
            <Row>
              <Form.Item label="为谁购买" required {...formItemStyle} {...povertyValidate}>
                <SmartSelectBox
                  editStatus
                  notEditableOnly={false}
                  onChange={this.setBuyPatient}
                  buttonOptions={releation_ship_list}
                  getPopupContainer={() => document.getElementById('creatPatientInsurance')}
                  value={selPatientReleation}
                />
              </Form.Item>
              {isSub && !selPatientReleation ? this.renderError() : null}
            </Row>
          ) : null}
          {patientFieldValue && selPatientReleation && selPatientReleation[0] != '1' && selre && selre.length ? (
            <Row>
              <Form.Item label="可选对象" required {...formItemStyle}>
                <SmartSelectBox
                  editStatus
                  notEditableOnly={false}
                  buttonOptions={
                    selre.length &&
                    selre.map((item) => {
                      return {
                        id: String(item.insuredId),
                        name: item.insuredName,
                        isDisabled: item.isDisabled,
                      };
                    })
                  }
                  onChange={this.selectBuyPatient}
                  getPopupContainer={() => document.getElementById('creatPatientInsurance')}
                  value={choseInsurancePatient}
                />
              </Form.Item>
              {isSub &&
              !selPatientRelValue &&
              selre &&
              selre.length &&
              selPatientReleation &&
              selPatientReleation[0] != '1'
                ? this.renderError()
                : null}
            </Row>
          ) : null}
          {selPatientReleation && selPatientReleation[0] != '1' ? (
            <Row>
              <Form.Item field="relpatientId" label="服务对象" required {...formItemStyle}>
                <div>
                  <SmartSelectSingleAsync
                    {...mapPropsToFormItems}
                    className="-x-id-search"
                    placeholder="输入会员姓名/身份证号/手机号/其他联系方式"
                    showSearch
                    value={selPatientRelValue}
                    filterOption={false}
                    delay
                    asyncResultId="addMedicineRegister.ChoosePatientRelation"
                    asyncRequestFuncName="searchPatient"
                    onChange={this.handlePatientRelChange}
                    onSelect={this.handlePatientRelSelect}
                    cleanOptionsOnBlur
                    getPopupContainer={() => document.getElementById('creatPatientInsurance')}
                    asyncMapResultToState={(data, params) => (params.keyWord ? data : undefined)}
                    mapDataToOption={this.mapDataToOption}
                    notEditableOnly={choseInsurancePatient ? true : false}
                  />
                  <div style={{ maxHeight: '30px', paddingTop: '10px' }}>
                    {selPatientRelDetails && selPatientRelDetails.gradeIcon ? (
                      <img src={selPatientRelDetails.gradeIcon} style={{ height: '30px' }} />
                    ) : null}
                    {selPatientRelDetails && selPatientRelDetails.certification ? (
                      <img src={PatientcertIficationImg} style={{ height: '30px' }} />
                    ) : null}
                  </div>
                </div>
              </Form.Item>
            </Row>
          ) : null}
          {patientFieldValue &&
          insurancePackageProList &&
          insurancePackageProList.length &&
          insurancePackageProList[0].guarantee !== null ? (
            <Row>
              <Form.Item label="有无社保" required {...formItemStyle} {...povertyValidate}>
                <SmartSelectBox
                  editStatus
                  notEditableOnly={false}
                  onChange={this.setguarantee}
                  buttonOptions={[
                    { id: '1', name: '有社保' },
                    { id: '0', name: '无社保' },
                  ]}
                  getPopupContainer={() => document.getElementById('creatPatientInsurance')}
                  value={guarantee}
                />
              </Form.Item>
              {isSub && !guarantee ? this.renderError() : null}
            </Row>
          ) : null}
          {patientFieldValue &&
          insurancePackageProList &&
          insurancePackageProList.length &&
          insurancePackageProList[0].poverty !== null ? (
            <Row>
              <Form.Item label="是否贫困" required {...formItemStyle} {...povertyValidate}>
                <SmartSelectBox
                  editStatus
                  notEditableOnly={false}
                  onChange={this.getInsuracePackage}
                  buttonOptions={[
                    { id: '1', name: '贫困' },
                    { id: '0', name: '非贫困' },
                  ]}
                  getPopupContainer={() => document.getElementById('creatPatientInsurance')}
                  value={poverty}
                />
              </Form.Item>
              <p style={{ position: 'absolute', left: '300px', top: '8px', color: '#F48E18' }}>
                提示：贫困证明材料可于出险理赔时提供
              </p>
              {isSub && !poverty ? this.renderError() : null}
            </Row>
          ) : null}
          {/* 新增销售渠道 */}
          {/* {
                        <Row>
                            <Form.Item label="销售渠道：" {...formItemStyle} >
                                {this.setCodeList()}
                            </Form.Item>
                        </Row>
                    } */}
          {selPatientReleation && arr.length > 0 ? (
            <Row>
              <Form.Item label="服务档次" required {...formItemStyle}>
                <SmartSelectBox
                  editStatus
                  notEditableOnly={false}
                  buttonOptions={arr.map((item) => {
                    return {
                      id: String(item.id),
                      name: item.gradeName,
                    };
                  })}
                  onChange={this.insuranceProChanged}
                  getPopupContainer={() => document.getElementById('creatPatientInsurance')}
                  value={insurancePackageProsId}
                />
              </Form.Item>
              {isSub && !insurancePackageProsId ? this.renderError() : null}
            </Row>
          ) : null}
          <Row>
            <Form.Item label="服务费" {...formItemStyle}>
              <div>
                <p style={{ textDecoration: 'line-through', color: 'rgba(0,0,0,0.40)' }}>
                  {insurancePackagePro &&
                  ((insurancePackagePro.poverty === null && poverty === undefined) ||
                    (insurancePackagePro.poverty !== null && poverty !== undefined))
                    ? insurancePackagePro
                      ? `¥${(insurancePackagePro.salesPrice / 100).toFixed(2)}/年`
                      : null
                    : null}
                </p>
                <div style={{ fontWeight: '600', display: 'flex' }}>
                  {insurancePackagePro &&
                  ((insurancePackagePro.poverty === null && poverty === undefined) ||
                    (insurancePackagePro.poverty !== null && poverty !== undefined))
                    ? insurancePackagePro && totalPrice >= 0
                      ? `¥${(totalPrice / 100).toFixed(2)}/年`
                      : null
                    : null}
                  {insurancePackagePro &&
                  ((insurancePackagePro.poverty === null && poverty === undefined) ||
                    (insurancePackagePro.poverty !== null && poverty !== undefined)) ? (
                    insurancePackagePro && totalPrice >= 0 && patientDetails && patientDetails.gradeIcond ? (
                      <div style={{ paddingLeft: '10px', marginTop: '3px' }}>
                        <img src={patientDetails.gradeIcon} style={{ height: '20px' }} />
                      </div>
                    ) : null
                  ) : null}
                </div>
              </div>
            </Form.Item>
          </Row>
          <Row>
            <Form.Item label="授权确认书" {...formItemStyle}>
              <div className="clearfix" style={{ display: 'flex' }}>
                <Upload
                  listType="picture-card"
                  accept="image/png, image/jpeg"
                  fileList={this.state.authorizationPicList}
                  onPreview={this.handlePreview}
                  onChange={(fileObj) => this.handleChange(fileObj, 'authorizationPicList')}
                  beforeUpload={this.handleBeforeUpload}
                  customRequest={this.customRequest}
                  disabled={!this.patientId}
                  action="_api/uploadImages"
                  data={this.uploadFile}
                >
                  {this.state.authorizationPicList.length >= 5 ? null : uploadButton}
                </Upload>
                <div style={{ marginTop: '75px' }}>（上限5张）</div>
              </div>
            </Form.Item>
          </Row>
          <Row>
            <Form.Item label="缴费方式" {...formItemStyle}>
              <SmartSelectBox
                editStatus
                notEditableOnly={false}
                onChange={(v) => this.payWayOnChange(v)}
                buttonOptions={[
                  { id: '1', name: '年缴' },
                  // { id: '2', name: '月缴' },
                ]}
                getPopupContainer={() => document.getElementById('creatPatientInsurance')}
                value={payWay}
              />
            </Form.Item>
            {isSub && !payWay ? this.renderError() : null}
          </Row>
          <Row>
            <Form.Item label="缴费金额" required {...formItemStyle}>
              <div>
                <div style={{ fontWeight: '600', display: 'flex', color: 'red' }}>
                  {insurancePackagePro && payWay && payWay.length > 0 && payWay[0] === '2'
                    ? payPrice >= 0
                      ? `¥${(payPrice / 100).toFixed(2)}/月`
                      : null
                    : insurancePackagePro && payPrice >= 0
                    ? `¥${(payPrice / 100).toFixed(2)}/年`
                    : null}
                </div>
              </div>
            </Form.Item>
          </Row>
          {payWay && payWay.length > 0 && payWay[0] === '2' ? (
            <Row>
              <Form.Item label="缴费周期" required {...formItemStyle}>
                <div>
                  <div style={{ fontWeight: '600', display: 'flex', color: 'red' }}>12个月</div>
                </div>
              </Form.Item>
            </Row>
          ) : null}
          <Row>
            <Col span={4} className="ant-form-item-label">
              <label className="ant-form-item-required" title="支付金额">
                支付金额
              </label>
            </Col>
            <Col span={20}>
              <Row>
                <Col span={4}>
                  <Form.Item label="" {...formItemStyle}>
                    <Checkbox.Group
                      onChange={(v) => this.countOnchange(v, 'isPoint')}
                      style={{ width: '100%' }}
                      value={isPoint}
                    >
                      <Checkbox
                        value={1}
                        disabled={
                          (payWay && payWay.length > 0 && payWay[0] === '2') ||
                          (patientDetails ? patientDetails.points : 0) <= 0 ||
                          payPrice <= 0
                        }
                      >
                        福利值抵扣
                      </Checkbox>
                    </Checkbox.Group>
                  </Form.Item>
                </Col>
                <Col span={8} style={{ paddingTop: '5px' }}>
                  {isPoint && isPoint.length > 0 && isPoint[0] === 1
                    ? `${(payPoint / 100).toFixed(2) || 0}（¥${((payPoint || 0) / 100).toFixed(2)}）`
                    : ''}
                </Col>
                <Col span={8} style={{ paddingTop: '5px' }}>
                  当前可用福利值：
                  {((patientDetails && patientDetails.points ? patientDetails.points : 0) / 100).toFixed(2)}
                  （折合¥
                  {patientDetails && patientDetails.points ? (patientDetails.points / 100).toFixed(2) : `0.00`}）
                </Col>
              </Row>
              <Row>
                <Col span={4}>
                  <Form.Item label="" {...formItemStyle}>
                    <Checkbox.Group
                      onChange={(v) => this.countOnchange(v, 'isInsuranceAccount')}
                      style={{ width: '100%' }}
                      value={isInsuranceAccount}
                    >
                      <Checkbox
                        value={1}
                        disabled={
                          (payWay && payWay.length > 0 && payWay[0] === '2') ||
                          (insuranceAccountInfo ? insuranceAccountInfo.balance : 0) <= 0 ||
                          payPrice <= 0
                        }
                      >
                        会员账户
                      </Checkbox>
                    </Checkbox.Group>
                  </Form.Item>
                </Col>
                <Col span={8} style={{ paddingTop: '5px' }}>
                  {isInsuranceAccount && isInsuranceAccount.length > 0 && isInsuranceAccount[0] === 1
                    ? `¥${((payInsuranceAccount || 0) / 100).toFixed(2)}`
                    : ''}
                </Col>
                <Col span={8} style={{ paddingTop: '5px' }}>
                  当前可用余额：¥
                  {insuranceAccountInfo && insuranceAccountInfo.balance
                    ? (insuranceAccountInfo.balance / 100).toFixed(2)
                    : '0.00'}
                </Col>
              </Row>
              {payWay && payWay.length > 0 && payWay[0] === '2' ? (
                <Row>
                  <Col span={24} style={{ color: 'grey', lineHeight: '55px' }}>
                    登记成功后，自动扣除首月金额，后期将由系统自动扣费，扣费完成后会短信通知会员或在微信端查看
                  </Col>
                </Row>
              ) : null}

              {
                <Row>
                  <Col span={4}>
                    <Form.Item label="" {...formItemStyle}>
                      <Checkbox.Group
                        onChange={(v) => this.countOnchange(v, 'isCash')}
                        style={{ width: '100%' }}
                        value={isCash}
                        disabled={is3dCash && is3dCash[0] === 1}
                      >
                        <Checkbox value={1}>W现金</Checkbox>
                      </Checkbox.Group>
                    </Form.Item>
                  </Col>
                  <Col span={8} style={{ paddingTop: '5px' }}>
                    {isCash && isCash.length > 0 && isCash[0] === 1 ? `¥${((payCash || 0) / 100).toFixed(2)}` : ''}
                  </Col>
                </Row>
              }
              {
                <Row>
                  <Col span={8}>
                    <Form.Item label="" {...formItemStyle}>
                      <Checkbox.Group
                        onChange={(v) => this.countOnchange(v, 'is3dCash')}
                        style={{ width: '100%' }}
                        value={is3dCash}
                        disabled={isCash && isCash[0] === 1}
                      >
                        <Checkbox value={1}>第三方-渠道收款</Checkbox>
                      </Checkbox.Group>
                    </Form.Item>
                  </Col>
                  <Col span={8} style={{ paddingTop: '5px' }}>
                    {is3dCash && is3dCash.length > 0 && is3dCash[0] === 1
                      ? `¥${((pay3dCash || 0) / 100).toFixed(2)}`
                      : ''}
                  </Col>
                </Row>
              }
            </Col>
          </Row>

          <Row>
            <Col span={4} className="ant-form-item-label">
              <label title="备注：">备注</label>
            </Col>
            <Col span={20}>
              <TextArea value={this.state.remarks} onChange={this.setRemark} rows={3} maxLength={20} />
            </Col>
          </Row>
        </Form>
      </Spin>
    );
  }

  // 渲染大家保险的同意书
  renderDjAgreement() {
    const { patientDetails = {} } = this.state;

    return `
            <div className="dj-agree">
                <div className="title">被保险人同意投保声明书</div>
                <div className="dj-strong">大家养老保险股份有限公司：</div>
                <div>本人（姓名${patientDetails.name}证件号码${
      patientDetails.idCard
    }）已就保险投保事宜与投保人进行了沟通和了解，并已认真阅读保险条款、责任免除，同意投保人为本人投保本保险产品、认可保险金额并认可以下参保内容。</div>
                <div className="dj-strong">问题一：是否有不能投保的疾病种类（第1、7条）：  ¨是  þ否</div>
                <div className="dj-strong">问题二：是否有以下列示的既往症（第5条）：       ¨是  ¨否</div>
                <div className="dj-txt">
                    <span className="dj-t-title">1、被保险人在投保前患有以下疾病的不能投保。</span>疾病种类：恶性肿瘤、艾滋病、精神和行为障碍（以世界卫生组织颁布的《疾病和有关健康问题的国际统计分类（ICD-10）》中F00-F99为准）、脑血管畸形、瘫痪、良性脑肿瘤、脑血管瘤、肺淋巴管肌瘤病、脊髓内肿瘤、室壁瘤、植物人状态、系统性红斑狼疮、嗜铬细胞瘤，肿瘤标志物异常。若投保前被保险人已患上述疾病但未如实告知而承保的，因任何疾病发生理赔时保险公司均不承担保险责任，并需投保人对该被保险人进行减人处理，退还该被保险人保险责任终止之日的现金价值。
                </div>
                <div className="dj-txt">
                    <span className="dj-t-title">2、保险期间：</span>1年
                </div>
                <div className="dj-txt">
                    <span className="dj-t-title">3、等待期：</span>被保险人首次投保或非连续投保时，重大疾病医疗保险责任等待期为60天
                </div>
                <div className="dj-txt">
                    <span className="dj-t-title">4、保险方案</span>
                    <table border="1px solid black" cellpadding="5px" cellspacing="0">
                        <tr>
                            <td>条款</td>
                            <td>重疾种类</td>
                            <td>免赔额</td>
                            <td>赔付比例</td>
                        </tr>
                        <tr>
                            <td>《大家团体重大疾病医疗保险》</td>
                            <td>100种</td>
                            <td>年度免赔额为5 万元（通过基本医疗保险、政府主办补充医疗和公费医疗获得的补偿，不可用于抵扣免赔额，从其他途径获得的医疗费用补偿可用于抵扣免赔额）</td>
                            <td>社保范围内自付部分70%；未持卡就医，则社保内费用按应赔付比例的60%进行赔付；社保范围外部分70%。</td>
                        </tr>

                    </table>
                </div>
                <div className="dj-txt">
                    <div><span className="dj-t-title"> 5、既往症：</span>被保险人在保险合同生效日前已患如下一种或多种疾病及其并发症，并因此导致重大疾病而产生的医疗费用，保险公司不承担保险责任：</div>
                    <div>①肝肾疾病：肾功能不全肾衰竭期和尿毒症期、肝硬化失代偿期</div>
                    <div>②心脑血管疾病：缺血性心脏病、慢性心功能不全（心功能三级以上）、脑梗死、脑出血、高血压3级（≥180/110mmHg）</div>
                    <div>③肺部疾病：慢性阻塞性肺疾病肺功能III级和IV级、慢性呼吸衰竭</div>
                    <div>④其他疾病：再生障碍性贫血、重大器官移植、HIV感染</div>
                </div>
                <div className="dj-txt">
                    <span className="dj-t-title">6、除外医疗费用：</span>除条款约定的责任免除外，保险公司不承担ECMO、器官移植、质子重离子、PET-CT、PET-MRI产生的相关费用
                </div>
                <div className="dj-txt">
                    <div><span className="dj-t-title">7、被保险人的职业非以下拒保类职业：</span></div>
                    <div>①专业技术人员：海上救助打捞工程技术人员（参与海上搜救、打捞等行动），紧急救助员，特技演员，高空杂技飞车飞人演员，替身演员（从事普通演员不能完成的武打镜头，高难度动作等工作），职业拳击运动员，角力运动员，空手道运动员，柔道运动员及教练，潜水运动员及教练，赛车运动员及教练，滑翔运动员及教练，跳伞运动员及教练，跆拳道运动员，冲浪运动员及教练，战地记者，动物园驯兽师，水产捕捞及相关人员，坑探工，地震物探爆破工，海洋地质取样工，矿物开采人员，钻井人员，石油、天然气开采人员，盐业生产人员，炼铁人员，无机化工产品生产人员，火药、炸药制造人员，动力设备装配人员，枪炮、弹药制造人员，火工品制造人员，航空产品装配与调试人员，电力设备安装人员，电力设备检修人员，水泥及水泥制品生产加工人员，石棉制品工，烟花爆竹制作工，土石方施工人员，施工架子搭设人员，安装玻璃幕墙工人，水电工(地下)，电梯、升降机安装工人（高空作业），铁路、地铁运输机械设备操作及有关人员，潜水员，航道航务施工人员</div>
                    <div>②军人：特种兵（海军陆战队、伞兵、水兵、爆破兵、蛙人、化学兵、负有布雷爆破任务的工兵、情报单位负有特殊任务者），空军飞行官兵、空军海洋巡弋舰及潜艇官兵，前线军人，武警内卫部队官兵（参与处置暴乱、暴力犯罪、恐怖袭击等事件），武警黄金部队官兵，武警水电部队官兵，武警交通部队官兵，边防部队官兵</div>
                    <div>③其他人员：特殊运动班学生（拳击、摔跤、跆拳道等）</div>
                </div>
                <div className="dj-sm1">特此声明。</div>
                <div className="dj-sm2"> 声明人签字：${patientDetails.name}</div>
                <div className="dj-sm2"> 声明人日期：${new Date().toLocaleDateString()}</div>

            </div>
        `;
  }

  // 询问弹窗  问询弹窗
  renderThirdPage() {
    const {
      insurancePackageList,
      isSubmiting,
      diseasesList,
      cansub,
      selPatientRelDetails,
      patientDetails,
      selPatientReleation,
    } = this.state;
    const sex =
      selPatientReleation && selPatientReleation[0] == '1'
        ? patientDetails && patientDetails.sex
          ? 1
          : 0
        : selPatientRelDetails && selPatientRelDetails.sex == 1
        ? 1
        : 0;
    return (
      <Form id="creatInsuranceDisease">
        {diseasesList && diseasesList.length
          ? diseasesList.map((item, index) => {
              return (
                <div key={index}>
                  <Title text={item.key + '、' + item.question} left={5} />
                  {item.answerDes &&
                    item.answerDes.map((dItem, index) => {
                      return (
                        <div key={'asd' + index}>
                          <div className="des-title">{dItem.title}</div>
                          <div className="des-cont">
                            {dItem.content.map((dcItem, dcIndex) => {
                              return <div key={'dItem' + dcIndex}>{dcItem}</div>;
                            })}
                          </div>
                        </div>
                      );
                    })}
                  <Row>
                    <Form.Item label="" {...formItemStyle}>
                      <SmartSelectBox
                        editStatus
                        multiple
                        notEditableOnly={false}
                        buttonOptions={item.answer.map((items, indexs) => {
                          if (items.sex_status === undefined || items.sex_status == sex) {
                            return {
                              id: String(indexs),
                              name: items.name,
                            };
                          }
                        })}
                        onChange={(val) => this.setqa(val, index, item)}
                        getPopupContainer={() => document.getElementById('creatPatientInsurance')}
                        value={this.state.q_list && this.state.q_list[item.id]}
                      />
                    </Form.Item>
                  </Row>
                  {this.state.q_list && this.state.q_list[item.id] && this.state.q_list[item.id].length
                    ? item.type === 'forbidden' &&
                      item.answer &&
                      item.answer[Number(this.state.q_list[item.id][0])] &&
                      item.answer[Number(this.state.q_list[item.id][0])].value !== 'NONE'
                      ? this.renderThirdError(index)
                      : item.type === 'warning'
                      ? this.renderWarn(this.state.q_list[item.id], index)
                      : null
                    : null}
                </div>
              );
            })
          : null}
      </Form>
    );
  }

  render() {
    const { location_href, isSubmiting, needHealthq } = this.state;
    // 弹窗滚动到顶部
    // let modalWrapper = document.querySelector('.ant-modal-wrap ')
    // modalWrapper && (modalWrapper.scrollTop = 0)
    return (
      <div>
        <Modal
          title={this.state.dlgTitle}
          visible={true}
          width={900}
          maskClosable={false}
          style={{ top: 20 }}
          onCancel={this.hideGroupModal}
          footer={this.renderFooter()}
          ref={(el) => (this.modalEle = el)}
          className="create-patient"
          id="createPatientModal"
        >
          {location_href === 0
            ? this.renderSsownerPage()
            : location_href === 1
            ? this.renderIndexPage()
            : location_href === 2
            ? this.renderSecondPage()
            : location_href === 3
            ? this.renderThirdPage()
            : null}
          {/* {this.renderDjAgreement()} */}
        </Modal>
      </div>
    );
  }
}

const WrappedNormalLoginForm = Form.create({ name: 'creatPatientInsurance' })(CreatPatientInsuranceOrder);

export default connectModalHelper(connect(WrappedNormalLoginForm));
